邮递员客户端无法解析来自JAX-RS响应构建器的字符串响应 [英] Postman client not able to parse string response from JAX-RS response builder
问题描述
我正在开发基于球衣的REST应用程序,其中我正在使用如下所示的响应生成器:
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();
在邮递员&中执行此API时当它产生此错误时,我看到这样的东西:
When executing this API in postman & when it generates this error , i see something like this :
这仅在漂亮"格式模式下出现. Postman客户端或JAX-RS有问题吗?
this comes only in the Pretty formatting mode. Is it a problem with Postman client or JAX-RS?
Request标头&原始模式如下所示:
The Request header & the raw mode is shown below :
此处的"N"是消息字符串的第一个字符.原始人预览模式显示错误消息就可以了.
The "N" here is the first character of the message string. The Raw & Preview mode show the error message just fine.
推荐答案
由于您的API响应类型为application/json,因此Postman尝试将其解析为JSON并无法解析,因为它只是一个错误消息字符串.
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.
要解决此问题,您需要在服务器端以某些JSON对象的形式返回错误. 像这样:
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();
这篇关于邮递员客户端无法解析来自JAX-RS响应构建器的字符串响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!