简体   繁体   中英

Spring Data REST Bean Validation

Consider a Person entity with a property name that is annotated as @NotNull . Then a simple PersonRepository and this repo exposed with Spring Data Rest.

When I POST to create a new Person , if the name property is null a ValidationException occurs as expected. But what I actually get on the client is an Internal Server Error (500) and the message is a TransactionSystemException that happened much later in the exception chain.

What I'd expect to get is a Bad Request (400) with the actual ValidationException and all it's useful information so the client can know what's wrong with the posted data.

There seems to be a way to attach custom validators with SDR as explained here . But the thing is, this is not a custom validator, it's a standard bean validation that happens when the repository is asked to save data. So I'm not really sure how those two come together.

So questions:

  1. What are my options to let the client know what's wrong with the submitted data when using SDR?. Things like what fields are invalid and what's the error for each field would be awesome.
  2. Are there any examples about this anywhere?

Thanks a lot.

What you need is a proper ExceptionHandler , it will handle back end exceptions and send meaningful rich messages (json/xml) to the front end client.

Take a look a this git repository

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM