MVC没有返回json ...请求不可用

时间:2015-09-25 16:17:55

标签: mysql json spring

大家好我想在Spring + Mysql中返回带有rest http请求的userName。

控制器:

scrolledToPosition = false;

型号:

@Controller
@SessionAttributes("user")
public class UserController {

    @Autowired
    private UserService userService;

     @RequestMapping(method=RequestMethod.GET, value="/userss/{userName}")
     public @ResponseBody String getUserName(@PathVariable String userName) {
        return userName;
     }
}

我只想检索我的服务放置,删除,发布和获取请求映射连接我的模型到我的mysql数据库。我试图以这种方式提出请求: http://localhost:8080/portuzona/userss?userName=Arnoldo

但我明白了:

@Entity
@Table(name="users")
public class User {

    @Id
    @GeneratedValue
    private Long id;

    @NotEmpty
    @Size(min=4, max=20)
    private String userName;

    @NotEmpty
    private String firstName;

    @NotEmpty
    private String lastName;

    @NotEmpty
    @Size(min=4, max=8)
    private String password;

    @NotEmpty
    private String status;


    @NotEmpty
    @Email
    private String emailAddress;


    @NotNull
    @Past
    @DateTimeFormat(pattern="MM/dd/yyyy")
    private Date dateOfBirth;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }



    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }   
}

portuzona和用户被正确处理(html视图),但我无法进入我的控制器。

Log正确地说bean已经找到了表:

HTTP Status 404 - /portuzona/userss

type Status report

message /portuzona/userss

description The requested resource is not available.

正如用户在此处所要求的更多信息可以提供帮助..或者不是:

/WEB-INF/servlet.xml

sep 25, 2015 2:07:22 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: portuzona.users
sep 25, 2015 2:07:22 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [dateofbirth, password, firstname, emailaddress, id, username, lastname, status]

/WEB-INF/config/servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
    <servlet-name>usersHibernateServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/servletConfig.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>usersHibernateServlet</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/jpaContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

  <display-name>Archetype Created Web Application</display-name>
</web-app>

2 个答案:

答案 0 :(得分:1)

看起来你正在混合映射和路径变量。两个选项:

1:

@RequestMapping(method=RequestMethod.GET, value="/userss/{userName}")
public @ResponseBody String getUname(@PathVariable String userName) {
   return userName;
}

http://localhost:8080/portuzona/userss/Arnoldo

2:

@RequestMapping(method=RequestMethod.GET, value="/userss/yourmapping")
public @ResponseBody String getUserName(@RequestParam String username) {
    return username;
}

http://localhost:8080/portuzona/userss/yourmapping?username=Arnoldo

答案 1 :(得分:0)

使用http://localhost:8080/portuzona/userss?userName=Arnoldo

尝试以下操作
    Dim Db As Database, tDef As TableDef
    Set Db = CurrentDb
    Set tDef = Db.CreateTableDef("ARCH_CALL")      
    tDef.Connect = BuildLinkString(UserName)
    tDef.SourceTableName = CreateDataSourceString(BuildDataSourceString, "LinkedTableName")
    Db.TableDefs.Append tDef