Postman client not able to parse string response f

2019-09-02 12:25发布

问题:

I am developing a jersey based REST application wherein i am using the response builder as below :

return Response.status(Response.Status.UNAUTHORIZED).entity(e.getMessage()).build();

When executing this API in postman & when it generates this error , i see something like this :

this comes only in the Pretty formatting mode. Is it a problem with Postman client or JAX-RS?

The Request header & the raw mode is shown below :

The "N" here is the first character of the message string. The Raw & Preview mode show the error message just fine.

回答1:

As your API response type is application/json then Postman tries to parse it as JSON and fails to parse because it is simply an error message string.

To overcome that you need on server side return error in form of some JSON object. Like this:

JsonObject error = ...;//some how created
error.addField("descr",e.getMessage());
return Response.status(Response.Status.UNAUTHORIZED).entity(error).build();