我试图使用Spring 4使用Spring Boot(没有MVC的Tomcat)在一个宁静的服务中返回一个用户对象,但它并没有将完全对象解析为JSON。我的User.java:
@Entity
public class User implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private int id;
private String username;
private String password;
// getters and setters
}
我的服务:
@ResponseBody
@RequestMapping(value="/service/user/save", method=RequestMethod.PUT, produces="application/json;charset=ISO-8859-1", consumes="application/json;charset=ISO-8859-1")
public User addUser(@RequestBody User user) throws AppException
{
return facade.save(user); // returns a User with it's ID
}
我的JSON结果(没有ID):
{
username: "john",
password: "1234",
}
解析只发生在用户名和密码上。 但是,当我返回一个手动解析的String时,如:
@ResponseBody
@RequestMapping(value="/service/user/save", method=RequestMethod.PUT, produces="application/json;charset=ISO-8859-1", consumes="application/json;charset=ISO-8859-1")
public User addUser(@RequestBody User user) throws AppException
{
user = facade.save(user);
return new ObjectMapper().writeValueAsString(user);
}
工作正常,返回:
{
id: 23,
username: "john",
password: "1234",
}
我失踪了什么?