我正在尝试将 jax-rs 响应序列化为 json 字符串。
服务器的响应是 json,我从 jersey 客户端获取它:
Response resp = target.request().method("PUT", Entity.json(payloadBean))
其中 payloadBean 是我的 json 请求。一切正常,但我无法转换 json 字符串中的 resp 以记录它。
如果我尝试:
String s = EntityUtils.toString((HttpEntity) resp.getEntity());
我得到:
org.glassfish.jersey.client.internal.HttpUrlConnector cannot be cast to org.apache.http.HttpEntity
顺便说一下,如果我不转换为 HttpEntity,编译器会说:
toString (org.apache.http.HttpEntity) in EntityUtils cannot be applied to (java.lang.Object).
我的相关导入是:
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
有什么想法吗?
最佳答案
使用 resp.readEntity(String.class)
public abstract <T> T readEntity(Class<T> entityType)
Type Parameters:
T
- entity instance Java type.Parameters:
entityType
- the type of entity.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.
关于jax-rs - 将 InboundJaxrsResponse 转换为 json 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34873300/