Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Why @Valid annotation not working when used inside service method argument but works for controller

 
Ranch Hand
Posts: 644
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having this UserDto class which I want to put under @Valid

I have some requirements where I can't put validation on controller method, but I need to do it on the service method.


But this is not working. And if I put @Valid on the controller method, I do get the validation errors as expected.
Why so? Does serialization/deserialization has something to do with valition?
 
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Valid is processed by Spring MVC for MVC controllers, not for any other classes.
This is so that it can return a bad request (400) error on these validation failures automatically.
 
Puspender Tanwar
Ranch Hand
Posts: 644
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Salil Wadnerkar wrote:@Valid is processed by Spring MVC for MVC controllers, not for any other classes.
This is so that it can return a bad request (400) error on these validation failures automatically.


What solution do we have then? I want to validate the object in exactly same manner as Spring does. Does spring provide any support for that?
 
Salil Wadnerkar
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Spring AOP for your non-Spring MVC related beans: https://www.baeldung.com/javax-validation-method-constraints
 
Bartender
Posts: 1220
38
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Spring Boot  you could annotate with @Validated your @Service class to get all @Valid annotated params in methods to be validated.
 
Puspender Tanwar
Ranch Hand
Posts: 644
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Claude Moore wrote:In Spring Boot  you could annotate with @Validated your @Service class to get all @Valid annotated params in methods to be validated.


That changes the returned response. The "object" and "field" returned in the JSON are different with @Validated. The "object" now contains "myUserService" and the "field" conatins "myUserService.nethod.name"
 
Puspender Tanwar
Ranch Hand
Posts: 644
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem solved using javax.validation.Validator.validate(object) which gives the failed constraints.
 
Claude Moore
Bartender
Posts: 1220
38
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad you solved, anyway sounds strange that using @Validated on a Service will cause the returning json to be modified.
Could you post some code excerpt ?
 
Claude Moore
Bartender
Posts: 1220
38
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, done some tests: please consider following code:



What makes the difference is that the first method is handled directly by Spring, which return a quite complex response with a full trace of the validation error. Using a service, when validation isn't satisfied an exception is thrown, which is in turn returned to the caller as a much simpler JSON response.
So, which approach is best ? I'd vote for putting the validation logic in a @Service, if present: this way, one could catch the exception and handle it the way he prefers.
 
We're all out of roofs. But we still have tiny ads:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!