0

We are using beanValidation-2.0 and cdi-2.0 under Open Liberty 20.0.0.3. This works fine in that Open Liberty returns a 400 Bad Request response containing the bean validation error message when a a bean validation error is detected. I would like to update our application to instead return a custom response when Open Liberty detects a bean validation error. Is there a way that I can somehow "intercept" the bean validation error (or the Open Liberty generated response) and return my own custom response?`

Geoff Alexander
  • 419
  • 5
  • 13

2 Answers2

1

Usually when you get a validation error, a ConstraintViolationException is thrown and you should be able to catch that and create your own response.

If you're using JAX-RS, you might want to do this by creating and registering an ExceptionMapper to handle this type of exception and turn it into a response. There's an example here.

Azquelt
  • 1,380
  • 10
  • 15
  • Thanks Azquelt. Your solution works like a charm. I was able to create a custom exception mapper class implementing ExceptionMapper that returns a custom response. – Geoff Alexander Apr 27 '22 at 13:36
0

You might be able to do this with a custom MessageInterpolator. It can be specified via the message-interpolator element of validation.xml.

njr
  • 3,399
  • 9
  • 7
  • Thanks for the MessageInterpolator suggestion. I'm not familiar with MessageInterpolator and plan to do my own searching; but I'd greatly appreciate any references/examples on how a custom MessageInterpolator could be used to generate a custom response, especially in the context of Open Liberty, you (or anyone else) are aware of. – Geoff Alexander Apr 25 '22 at 19:36