我正在使用以下代码来使用休息后服务
Client client = ClientBuilder.newClient();
WebTarget target = client.target("wrong url");
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);
Response response = builder.post(Entity.entity(param, MediaType.APPLICATION_JSON), Response.class);
正如预期的那样,我收到了错误。和状态代码 400 错误请求
。但我没有收到错误消息。当我运行response.getStatusInfo()时,我收到错误的请求,但我的服务器发送了额外的信息。
当我使用 postman
调用它时,我在正文窗口中收到错误信息。
那么如何从响应对象中获取此错误正文信息? 或者任何其他方式???
最佳答案
您可以像往常一样使用 Response#readEntity 获得响应正文:
Read the message entity input stream as an instance of specified Java type using a
MessageBodyReader
that supports mapping the message entity stream onto the requested type.
示例:
Client client = ClientBuilder.newClient();
WebTarget target = client.target("wrong url");
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);
Response response = builder.post(Entity.entity(param, MediaType.APPLICATION_JSON), Response.class);
String body = response.readEntity(String.class);
与 Response#getStatusInfo你只得到 HTTP status , class和 reason phrase .
关于java - Jersey 客户端获取错误消息正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35128467/