Skip to content
Advertisement

How to make a custom message from an already defined response on Spring

My API is returning me this:

JavaScript

The problem is, I would like to, on the array that is represented by the second ID, return a error message, as there is no data in it. Something like this:

JavaScript

My code is as follows:

JavaScript

With this, I can’t seem to override the specific response, as it is an List of the Entity(Health).

Thanks for your help!

Advertisement

Answer

What you want to do is a really bad idea since one would expect all elements in a JSON array to have the same attributes.

You should return what you are returning now with all the nulls and return your “message” in status.

JavaScript

I would also recommend adding a “statusCode” attribute where you can return a numerical code and have “status” represent the statusCode description since doing string compares on status isn’t a good idea either.

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