Java Jersey:读取内容类型:纯文本响应为XML
我收到一个来自外部服务器的Java Jersey:读取内容类型:纯文本响应为XML,java,xml,jaxb,jersey,mime-types,Java,Xml,Jaxb,Jersey,Mime Types,我收到一个来自外部服务器的HTTP响应,该服务器的正文中包含XML 但是,响应标题显示内容类型:纯/文本 这是不正确的,应该是application/xml。但是,正如我所说,它是一个外部服务器,我们无法更改 以下代码给出了一个错误: ClientResponse response = Client.create().resource(url).get(ClientResponse.class); return response.getEntity(XmlResponse.class); 例外
HTTP
响应,该服务器的正文中包含XML
但是,响应标题显示内容类型:纯/文本
这是不正确的,应该是application/xml
。但是,正如我所说,它是一个外部服务器,我们无法更改
以下代码给出了一个错误:
ClientResponse response = Client.create().resource(url).get(ClientResponse.class);
return response.getEntity(XmlResponse.class);
例外情况:
com.sun.jersey.api.client.clienthandler异常:消息正文
Java类com.evs.ats.XmlResponse和Java类型类的读取器
com.evs.ats.XmlResponse,未找到MIME媒体类型text/plain
以下代码可以工作,但我不喜欢它:
String resultString = response.getEntity(String.class);
InputStream stream = new ByteArrayInputStream(
resultString.getBytes(StandardCharsets.UTF_8)
);
JAXBContext jc = JAXBContext.newInstance(XmlResponse.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
return (XmlResponse) unmarshaller.unmarshal(stream);
有没有办法“强迫”Jersey将其理解为
XML
?或者,我唯一可以使用JAXB
(如上所述)手动解组它的选项是什么?或者还有其他选择吗?如果您连接的上游服务器在内容类型
HTTP头字段中返回错误的值,并且无法“推理”(修复返回的值),那么除了提供自己的MessageBodyReader
自定义实现之外,没有其他方法(AFAIK)
使用球衣2的一种方法是:
public class TextAsXmlMessageBodyReader implements MessageBodyReader<Object> {
@Override
public boolean isReadable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return mediaType.getType().equals("text")
&& mediaType.getSubtype().equals("plain");
}
@Override
public Object readFrom(Class<Object> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
InputStream entityStream) throws IOException, WebApplicationException {
try {
final JAXBContext jc = JAXBContext.newInstance(type);
final Unmarshaller unmarshaller = jc.createUnmarshaller();
return unmarshaller.unmarshal(entityStream);
} catch (JAXBException e) {
throw new WebApplicationException("Error while unmarshalling the response.", e);
}
}
}
拥有自定义读卡器实现的优点是,可以编写漂亮的流线型代码,重点是配置和发出请求、获取响应,以及在需要时对其进行处理。此外,如果您需要对目标服务器上的不同资源发出请求(并且所有资源都指定了错误的
内容类型),则可以重用读卡器实现。如果您连接的上游服务器在内容类型HTTP头字段中返回错误值,并且无法“推理”(要修复返回的值),除了提供自己的MessageBodyReader
自定义实现之外,没有其他方法(AFAIK)
使用球衣2的一种方法是:
public class TextAsXmlMessageBodyReader implements MessageBodyReader<Object> {
@Override
public boolean isReadable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return mediaType.getType().equals("text")
&& mediaType.getSubtype().equals("plain");
}
@Override
public Object readFrom(Class<Object> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
InputStream entityStream) throws IOException, WebApplicationException {
try {
final JAXBContext jc = JAXBContext.newInstance(type);
final Unmarshaller unmarshaller = jc.createUnmarshaller();
return unmarshaller.unmarshal(entityStream);
} catch (JAXBException e) {
throw new WebApplicationException("Error while unmarshalling the response.", e);
}
}
}
拥有自定义读卡器实现的优点是,可以编写漂亮的流线型代码,重点是配置和发出请求、获取响应以及在需要时对其进行处理。此外,如果需要对目标服务器上的不同资源发出请求,则可以重用读卡器实现(并且所有这些都指定了错误的内容类型
)。哪个版本的jersey?com.sun.jersey客户端1.8
哪个版本的jersey?com.sun.jersey客户端1.8