I have a post endpoint in my controller in spring that looks like so
@Autowired
private BookRepository bookRepository;
@PostMapping(path="/book")
public @ResponseBody BookEntity addBook(@RequestBody BookEntity bookEntity)
{
return this.bookRepository.save(bookEntity);
}
Now, if invalid json data is submitted and the save function is called my console will output an error such as h.engine.jdbc.spi.SqlExceptionHelper : Duplicate entry '1091209' for key 'isbn'
And the user will see a very non-user friendly api response.
Another issue this brings it seems is that an auto_incremented id is skipped e.g. I go 1,2,4, skipping 3 due the 3rd api call being the failing api call.
What is best practice for handling the above scenarios?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…