0

I know this issue has been around there in other post, but after applying the fiux suggested was not working.

I am using spring 4.1.7 version, i want to validate the RequestBody from post rest call. For doing this i tried following set of codes, but it was not working as i expected.

My Request body pojo classes.

ParentPojo.class

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
@Validated
public class ParentPojo<T> implements Serializable{
    @NotNull
    private String  cNumber;
    @NotNull
     private String  statusCode;
     @NotNull T child;
}

ChildPojo.class

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
@Validated
public class ChildPojo{

    @NotNull
    private String name;

    @NotNull
    private String address;

    @NotNull
    private String pin;

}

Controller:

Adding only methods

@Autowired
    @Qualifier("validator")
    private Validator validator;

    @InitBinder
    private void initBinder(WebDataBinder binder) {
        binder.setValidator(validator);
    }

 @RequestMapping(produces = { "application/json", "application/xml" }, consumes ={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, method = { RequestMethod.POST })
    @ResponseStatus(HttpStatus.CREATED)
    public Messageresponse<ChildPojo> create(@NotNull(groups = {ParentPojo.class, ChildPojo.class}) 
    @Valid @Validated({ParentPojo.class, ChildPojo.class}) @RequestBody final ParentPojo<ChildPojo> ParentPojo, BindingResult bindingResult) {
       System.out.println("new version 8="+bindingResult.hasErrors());
       validator.validate(ParentPojo, bindingResult);
       if(bindingResult.hasErrors()) {
           System.out.println("Non formated form stuff.");
       }

        return service.create(ParentPojo);
    }

 @RequestMapping(value = "/{create}", produces = { "application/json", "application/xml" }, consumes ={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, method = { RequestMethod.POST })
    @ResponseStatus(HttpStatus.CREATED)
    public Messageresponse<ChildPojo> create1(@NotNull @Valid @RequestBody final ParentPojo<ChildPojo> ParentPojo, BindingResult bindingResult) {
       System.out.println("new version 8="+bindingResult.hasErrors());
       validator.validate(ParentPojo, bindingResult);
       if(bindingResult.hasErrors()) {
           System.out.println("Non formated form stuff.");
       }

        return service.create(ParentPojo);
    }

application context xml:

<mvc:annotation-driven validator="validator">
    </mvc:annotation-driven>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

jar file tried:

hibernate-validator-4.3.0.Final
hibernate-validator-4.1.0.Final
hibernate-validator-4.0.2.GA
validation-api-1.1.0.Final
spring-context-4.1.7.RELEASE

But nothing was working with all the above combination for the request below:

in below request "pin" is missed and the controller method @Valid @RequestBody expected to handle this request as Bad Request. instead it is accepting the request body and processing further.

{
"cNumber" : "ff",
"statusCode" : "ddd",
"child" : {
"name" : "ll",
"address" : "ll"
}
user2000189
  • 479
  • 4
  • 6
  • 22

1 Answers1

0

Look at this question Here

You need to decorate the child pojo as @Valid

public class ParentPojo<T> implements Serializable{
   @NotNull
   private String  cNumber;
   @NotNull
   private String  statusCode;
   @Valid 
   @NotNull 
   T child;
}
Amanuel Nega
  • 1,899
  • 1
  • 24
  • 42