3

I have the following code in my controller

@RequestMapping(value = "employee/update", method = RequestMethod.POST, headers = "Accept=application/json")
    public UpdateEmployeeResponse updateEmployee(@RequestBody @Valid @ModelAttribute("updateEmployeeRequest") UpdateEmployeeRequest updateEmployeeRequest, BindingResult result) {

My Request object is as follows

public class UpdateEmployeeRequest {
@Valid
@NotNull
private Employee employee;
.
.

public class Employee {
@NotNull
protected String id;
@NotNull
protected String name;
.
.

When I send JSON request like (id is missing)

{employee:{name:"cc",phone:"9876543210",dept:"dpt"}}

My Request is not getting validated by spring(it doesn't show any error even if a field is missing). I have gone through the following threads but no luck.

Can anyone help?

Community
  • 1
  • 1
arjuncc
  • 3,227
  • 5
  • 42
  • 77

2 Answers2

1

To ignore any unknown properties in JSON input without exception try using @JsonIgnoreProperties(ignoreUnknown=true).

Try this out

Employee.java

@JsonIgnoreProperties(ignoreUnknown = true)
public class Employee
{
 @NotNull
 protected String id;
 @NotNull
 protected String name;
 .
 .

UpdateEmployeeRequest.java

@JsonIgnoreProperties(ignoreUnknown = true)
public class UpdateEmployeeRequest {
 @Valid
 @NotNull
 private Employee employee;
 .
 .
Nidhish Krishnan
  • 20,593
  • 6
  • 63
  • 76
0

You don't mention whether you have a validator in the project. In Maven add these dependencies:

        <!-- Bean validation -->        
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.0.1.Final</version>
     </dependency>