2
votes

I am trying to integrate Bean Validation API - javax.validation and hibernate-validator with a Spring MVC application. I can tell that when the REST controller method is not using the BindingResult argument, the server starts up fine and works. But when I use BindingResult arg in the function signature, then I see runtime errors as the following

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

Code:

Controller method:

@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>

The above dependency will in turn pull javax.validation:validatior-api

UsersList is essentially a list of the following 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
Can you also please paste ur model UsersList - Albert Pinto
I have pasted it above Albert - Gayathri Vijayakumar
and in your request are you sending a null for firstname? - Albert Pinto
Yes I am not sending the firstName field at all. - Gayathri Vijayakumar

1 Answers

0
votes

Changing the to DispatcherServlet from SpringServlet in web.xml helped me get rid of this problem.