0

@Valid is not working with RequestBody in Sptring 3.0.5. Below is code snippets . Please suggest what is causing the issue.

pom.xml file

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.0.2.GA</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>

Conroller:

@RequestMapping(value = "/ValidateCustomer", method = RequestMethod.POST, headers="content-type=application/json")
    @ResponseBody
    public String validateCustomer(@Valid @RequestBody Customer customer) throws Exception {
// further processing"

return "Validated";
}

Customer class:

import org.hibernate.validator.constraints.NotEmpty;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;


public class Customer {
    
    
    @NotNull
    private String userId;
    
    @NotNull
    private String firstName;
    
    @NotNull
    private String lastName;
    
    @NotNull
    private String companyName;
/* getter and setter methods */
}

Below is the payload requested from postman which should fail to the request, but receiving success message.

{ "firstName":"abc", "lastName": "xyz", "companyName":"abc Corp" }

  • Does this answer your question? [spring rest @RequestBody does not validate with @Valid](https://stackoverflow.com/questions/70340575/spring-rest-requestbody-does-not-validate-with-valid) – madteapot Oct 13 '22 at 08:33
  • Seems @Validated not supported in Spring3.0.5 – user3454638 Oct 13 '22 at 09:19

0 Answers0