2015-08-16 27 views
0

我和春天有个开机,Spring MVC和hibarnate的项目,我想验证其余参数,可以我有控制器:如何设置验证(org.hibernate.validator.constraints)在spring引导项目中休息?

import domain.User; 
import service.UserService; 
import validate.Email; 
import org.apache.log4j.Logger; 
import org.hibernate.validator.constraints.NotEmpty; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 

import javax.ws.rs.FormParam; 
import java.util.ArrayList; 
import java.util.List; 

@Controller 
public class UserController { 

    @Autowired 
    private UserService userService; 

    @RequestMapping(value = "/registration", method = RequestMethod.POST) 
    public @ResponseBody Boolean registration(@NotEmpty @FormParam("login") String login, 
           @NotEmpty @FormParam("password") String password, 
           @Email(canBeNullOrEmpty = true) @FormParam("email") String email) { 
     logger.debug("method registration with params login = " + login 
       + ", password = " + password + ", email = " + email); 
     return !userService.findByLoginAndPassword(login, password) 
       && userService.addUser(new User(login, password, email)); 
    } 

} 

和我的build.gradle:

apply plugin: 'war' 
apply plugin: 'spring-boot' 

buildscript { 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.8.RELEASE") 
    } 
} 

ext { 
    springVersion = '4.1.5.RELEASE' 
    jacksonVersion = '2.5.3' 
} 

sourceCompatibility = 1.5 

repositories { 
    mavenCentral() 
} 

dependencies { 

    compile 'javax.validation:validation-api:1.1.0.Final' 
    compile "org.hibernate:hibernate-validator:5.1.3.Final" 

    compile "org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion" 

    compile 'javax.ws.rs:javax.ws.rs-api:2.0.1' 

    compile 'org.postgresql:postgresql:9.3-1101-jdbc41' 

    compile "com.fasterxml.jackson.core:jackson-core:$jacksonVersion" 
    compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion" 

    compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion" 

} 

所以,我写了登记为我的用户,并添加验证(@NotEmpty登录和密码,@电子邮件)为rest params,但验证不执行:(我发送请求与无效的参数密码=“”和请求运行没有错误:(为什么?我为密码设置了验证@NotEmpty,如果我设置了密码=“”或null,我必须看到错误!becouse设置验证。请帮助我。

回答

1

验证不在方法参数上执行。只需将这些移动到POJO,然后使用@Valid@ModelAttributes进行注释,那么您将会很好。

喜欢的东西

@RequestMapping(value = "/registration", method = RequestMethod.POST) 
public @ResponseBody Boolean registration(@Valid FooBar fooBar) { 
} 

static class FooBar { 

    @NotEmpty 
    String login; 

    @NotEmpty 
    String password; 

    @Email(canBeNullOrEmpty = true) 
    String email; 

    // getter & setter 

} 
+0

是否只是标注的参数'@ Valid'不行的,而不是参数移动到一个POJO?只是为了证实,因为我觉得匆匆回忆起我们曾经在某个项目中使用过@Valid。 – Sanjay

+0

当然你可以在方法参数中加上'@ Valid'(这就是触发验证的原因!)。但是你不能对方法参数施加约束(所以你不能把'@ NotNull'作为例子)。我会编辑我的答案,使其更清楚。 –

+0

也许我错误地回想起对方法参数施加约束。 – Sanjay

相关问题