spring-projects / spring-projects/spring-data-rest
Ease definition of exception handler for custom validation exception [DATAREST-1457]
@odrotbohm is already working on this.
Since Dec 31, 2020.
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
Florent Biville opened DATAREST-1457 and commented
I am currently developing a small application that demoes a simple back-office administration GUI for a fictional e-commerce application, as part of an upcoming larger riff demo. The sources are (temporarily) here: https://github.com/fbiville/inventory-management/.
As I am using Spring Data JDBC, I developed the entity validation directly in the constructor as you can see here.
The following test ensures that an invalid payload sent to the built-in "save" API will result in a 400 Bad Request.
However, if I do not customize the exception handling, my custom validation exception will be buried into the Jackson exception ValueInstanciationException which itself is the cause of HttpMessageNotReadableException. This ends up with quite an unideal payload with 3 nested cause JSON properties instead of the custom serialization format that I would get by directly serializing my exception (via a custom Jackson serializer).
I tried to define a custom @ExceptionHandler method of a @ControllerAdvice-annotated class, targetting specifically my custom exception type, but it was not being picked up at all.
I ended up overriding ResponseEntityExceptionHandler#handleHttpMessageNotReadable but the implementation is quite fragile.
I probably have missed something. Is there a better way?
No further details from DATAREST-1457
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.