I want to Insert JSON data object (User) to my Spring MVC application (via controller) with Jquery. if code is like follow how can I accomplish this? Thanks.
User Object
public class User {
private Integer usId;
private String usName;
private Boolean usActive;
}
Spring Controller
@Controller
@RequestMapping(value="/user")
public class UserController {
@Autowired
UserService userService;
private Validator validator;
@RequestMapping(method=RequestMethod.POST)
public @ResponseBody Map<String, ? extends Object> create(@RequestBody User user, HttpS开发者_JS百科ervletResponse response) {
Set<ConstraintViolation<User>> failures = validator.validate(user);
userService.insert(user);
if (!failures.isEmpty()) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return validationMessages(failures);
} else {
return Collections.singletonMap("id", user.getUsId());
}
}
}
$.post("/user", { usName: "John", usActive: "true" } );
精彩评论