Skip to content
Advertisement

Spring Boot – Jackson EntityNotFoundException returns 200 instead of 500 response

Exception handler is returning 200 response even though I have specified it to return 500 in the handler (HttpStatus.INTERNAL_SERVER_ERROR) when encountering this exception.

I am using Spring Boot v1.5.4.RELEASE.

I am calling a Spring Boot service returning a JSON object. I am using a custom exception handler.

When an EntityNotFoundException occurs during deserialization, instead of returning 500 response, it returns 200 response and embeds the error in the body output. This is caused by a data error where a ManyToOne relationship not being found in Database because the foreign key is invalid.

Note that I am using a native query to pull the entities.

JavaScript

How can I make it so the service returns a 500 response instead of 200?

Code Example:

JavaScript

Sample Output of Service:

Response Code: 200

Body:

JavaScript

#note: I did not cut this off just for this example. This is cut off in the actual json response resulting in badly formatted json.

Advertisement

Answer

You can avoid the entity not found error by inner joining in the query. Below example uses native query format. This is more of a workaround solution, but it was sufficient to resolve my issue for my situation. I am marking as answer for now.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement