设计一个restful url来验证对象

时间:2015-10-14 09:07:53

标签: java validation spring-mvc restful-url

是否可以从restful URL填充spring MVC中的java对象。

为了澄清,我可以制作一个网址吗? 本地主机:8080/user/email/a@a.com/password/aaa 这将填充User对象的电子邮件和密码字段并返回给我用户?

@RequestMapping(value = "user/email/{email}/password/{password}", method = RequestMethod.POST)
    public ResponseEntity<Users> createNewUserRegistration(@Valid User user) {

}

2 个答案:

答案 0 :(得分:0)

public ResponseEntity<Users> createNewUserRegistration(@PathVariable("email")String email,@PathVariable("password")String password) {
User user = new User(email,password);
//do validation etc
return user;
}

答案 1 :(得分:0)

好的,最后它对我有用。您需要将它添加到您的pom.xml

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.12</version>
</dependency>

@RequestMapping(value = "user", method = RequestMethod.POST)
    public ResponseEntity<Users> createNewUserRegistration(@RequestBody @Valid User user) {

}

并在请求正文中传递json

{
    "emailAddress":"email",
    "password":"password"
}