尝试将Bean验证API与Spring MVC集成时,运行时服务器启动错误

时间:2015-10-08 18:30:19

标签: spring hibernate spring-mvc

我正在尝试将Bean Validation API - javax.validation和hibernate-validator与Spring MVC应用程序集成。我可以告诉我,当REST控制器方法没有使用BindingResult参数时,服务器启动正常并且工作正常。但是当我在函数签名中使用BindingResult arg时,我将运行时错误视为以下

SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.gigsky.usermanagement.restapi.api.UserManagement.createUsers(com.gigsky.usermanagement.restapi.beans.UsersList,org.springframework.validation.BindingResult) throws java.lang.Exception at parameter at index 0
  SEVERE: Method, public javax.ws.rs.core.Response com.gigsky.usermanagement.restapi.api.UserManagement.createUsers(com.gigsky.usermanagement.restapi.beans.UsersList,org.springframework.validation.BindingResult) throws java.lang.Exception, annotated with POST of resource, class com.gigsky.usermanagement.restapi.api.UserManagement, is not recognized as valid resource method.
Oct 8, 2015 11:34:44 PM com.sun.jersey.spi.spring.container.servlet.SpringServlet initiate
SEVERE: Exception occurred when intialization

代码:

控制器方法:

@POST
    @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
    @Consumes(MediaType.APPLICATION_JSON + ";charset=UTF-8")
    public Response createUsers(@Valid UsersList usersList, BindingResult result) throws Exception {
        if(result.hasErrors()){
            System.out.println("Error");
        }
        else{
            System.out.println("Success");
        }
        userManagementService.createUsers(usersList);
        return Response.status(Response.Status.OK).build();
    }

的pom.xml

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.0.1.Final</version>
        </dependency>

上面的依赖关系将依次拉javax.validation:validatior-api

UsersList本质上是以下UserInfo的列表

public class UserInfo {
    @JsonProperty
    private String type = "User";
    @JsonProperty
    private String userId;
    @JsonProperty
    @NotNull
    private String firstName;
    @JsonProperty
    private String lastName;
    @JsonProperty
    private String emailId;
    @JsonProperty
    private String password;
    @JsonProperty
    private String location;
    @JsonProperty
    private Address address;
    @JsonProperty
    private String preferredCurrency;
    @JsonProperty
    private String userStatus;
    @JsonProperty
    private String passwordSet;
    @JsonProperty
    private String createdOn;
    @JsonProperty
    private String homePhone;
    @JsonProperty
    private String mobile;
    @JsonProperty
    private String department;
    @JsonProperty
    private List<UserAccountAssociation> list;

..... }

1 个答案:

答案 0 :(得分:0)

在web.xml中从SpringServlet更改为DispatcherServlet帮助我摆脱了这个问题。