I have created a custom exception handling. I thought I have made it worked because it shows the error page on localhost and it's giving the correct info when I used the postman. However, on my terminal it's giving me this error -> Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException$Conflict: 409 null] with root cause.
Any suggestions on how to resolve this? Thanks
(Please note that I have a Rest Controller class and Web controller classes.)
@ControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = MyDuplicateEntryException.class)
public ResponseEntity<MyErrorResponse> conflictErrorHandler(MyDuplicateEntryException e) throws IOException {
return new ResponseEntity<>(new MyErrorResponse(e.getMessage()), CONFLICT);
}
}
@ResponseStatus(CONFLICT)
public class MyDuplicateEntryException extends RuntimeException {
public MyDuplicateEntryException(String errorMessage) {
super(errorMessage);
}
}
On ServiceImpl:
@Override
public ResponseEntity<Artist> createArtist(AddArtistForm addArtistForm) throws DuplicateEntryException {
String artistName = addArtistForm.getName();
Optional<Artist> artist = artistRepository.findArtistByName(artistName);
artist.ifPresent(a -> {
throw new DuplicateEntryException(artist.get().getName() + " already exists");
});
return new ResponseEntity<>(artistRepository.save(new Artist(artistName)), CREATED);
}