0

I'm using Spring 4 and I need to validate my requests according to the type of the request.

I'm trying to validate my requests in Service layer(I don't know how can I do in controller or if I won't respect the single responsability pattern if I put this type of validation in Controllers layer).

Why?

I need to validate two or more types of requests like insert a new item, update, etc

Like:

@RequestMapping(value = "/insert", method = RequestMethod.POST)
public void insert(@RequestBody Entity entity) 

In this case I want to use InsertEntityValidator

@RequestMapping(value = "/update", method = RequestMethod.POST)
public void update(@RequestBody Entity entity) 

In this case I want to use UpdateEntityValidator

Custom validator is necessary because of Business Validation

I don't know the ways to do that.....

Lucas
  • 1,251
  • 4
  • 16
  • 34
  • See [this section](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-validation) of the reference guide. Or just call the validator manually. (In addition read [this javadoc](http://docs.spring.io/spring/docs/current/javadoc-api/index.html?org/springframework/validation/package-summary.html) on how you could do it using validation groups and JSR-303 validation). – M. Deinum May 23 '16 at 14:34
  • I saw the @InitBinder but this annotation fix a validator for my controller... how can I use two validators conditionally? – Lucas May 23 '16 at 14:38
  • Again read the documentation I pointed you at, as well as the javadoc. And as a last resort you can always manually invoke the validator... – M. Deinum May 23 '16 at 17:45
  • Possible duplicate of [How to use SpringMVC @Valid to validate fields in POST and only not null fields in PUT](http://stackoverflow.com/questions/33741883/how-to-use-springmvc-valid-to-validate-fields-in-post-and-only-not-null-fields) – manish May 23 '16 at 21:02

0 Answers0