• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to Throw a Custom Exception When Bean Validation In Spring Boot with Hibernate Validator Fails?

 
Ranch Hand
Posts: 46
Eclipse IDE Slackware Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using Spring Boot 2.0.1 with inclusion of validation-starter (which comes with Hibernate Validation 6.x) and trying to validate a method parameter and return objects in my Spring repository layer. There are basically a ParamObj and ReturnObj for this method,


ParamObj and ReturnObj both use a simple set of Bean Validation annotations on some of their properties, e.g @Pattern, @Min, etc.. My problem is that with these annotations, if either ParamObj or ReturnObj fails the validation process, MyRepoImpl#myMethod() would throw javax.validation.ConstraintViolationException.

However, as you can see, the contract for myMethod() mandates to throw a CustomException. Yes, ConstraintViolationException is a RuntimeException, however, now the caller of MyRepoImpl#myMethod(), e.g. a service layer logic, needs to catch this RuntimeException in addition to CustomException.

And the reason is, I need to prevent all the exceptions from bubbling out of the service layer (for this particular Spring @Service I have which calls this repository) -- this service is part of a long list of services that are called via a service orchestration which constructs a larger encompassing object. In other word, this piece (result coming obtained via MyRepoImpl) is considered optional, and subsequently, its failure should only be logged but do not stop the whole process.

For that reason, I can't utilize ControllerAdvice to capture ConstraintViolationException for the entire application.

My goal is to be able, when the validation fails for either method parameter or return, to throw only CustomException back to the caller.

Is this possible with annotations only? I looked into MethodValidationPostProcessor but can't figure out if I can implement my own to accomplish this goal.

P.S. The only way I can think to achieve this is to forgo @Valid/@Validated and call Validator#validate(obj, ...) programmatically for the parameter and return value inside the try-catch block, catch the constraint exception, and throw my CustomException(). However, I want to know if I can avoid that and leave this hard coded validation out of the repository.
 
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you catch the exception and then it doesn't come back as the right type of exception or what?   I don't think the Constraint violation is thrown inside the method which is why you aren't catching it.  Maybe I don't understand what your problem is.
 
Yalvin Duha
Ranch Hand
Posts: 46
Eclipse IDE Slackware Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Al Hobbs wrote:So you catch the exception and then it doesn't come back as the right type of exception or what?   I don't think the Constraint violation is thrown inside the method which is why you aren't catching it.  Maybe I don't understand what your problem is.



The exceptions are thrown (runtime) when validations fail. I would like to know if there is a way I can catch them in some sort of custom handler/interceptor associated only to this method and throw a new exception back to the caller.

Those @Valid AOPs/advises are handled by some interceptor/post processor. I would like to know if there is some sort of abstraction they provide that I can tell it to ultimately throw my CustomException when validation fails, rather than ConstraintViolationException variation.

That way, my callee (myMethod) in the repository is always guaranteed to throw CustomException back to the caller, e.g. a service call, rather than in case of validation some sort of Bean Validation constraint exception.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!