我有一个 REST 服务,它的 Response 对象如下:
@XmlRootElement(name="testresponse")
class Test{
private Map<String, String> prop = new TreeMap<>();
@JsonAnyGetter
@XmlTransient
public Map<String, String> getJSONProp() {
return prop;
}
@XmlAnyElement(lax = true)
@JsonIgnore
public List<JAXBElement<String>> getXMLProp() {
List<JAXBElement<String>> el = new ArrayList<>();
for (Map.Entry<String, String> entry : prop.entrySet()) {
el.add(new JAXBElement<>(new QName(entry.getKey()), String.class, entry.getValue()));
}
return el;
}
@JsonAnySetter
public void addProp(String key, String value) {
prop.put(key, value);
}
public void setProp(Map<String, String> prop) {
this.prop = prop;
}
}
getXMLProp() 方法用于 XML 响应,其中标签名称与 Map prop 的键匹配。getJSONProp 方法用于 JSON 响应。
当我尝试从邮递员客户端调用服务时,它工作正常,但是当我尝试使用 jersey 客户端调用服务时,响应没有数据。有人可以建议如何在响应中填充 JAXBElement 吗?
reponse.getJSONProp() 也会抛出以下错误:
java.lang.ClassCastException: org.apache.xerces.dom.ElementNSImpl cannot be cast to javax.xml.bind.JAXBElement
泽西版本:1.12。杰克逊版本:1.9.2
Jersey 客户端配置如下:
com.sun.jersey.api.client.config.ClientConfig jerseyConfig = new DefaultClientConfig();
jerseyConfig.getClasses().add(JacksonJaxbJsonProvider.class);
Client c = Client.create(jerseyConfig);
jersey 客户端中使用的 MediaType 是 MediaType.APPLICATION_XML。有人可以建议我在这里缺少什么吗?提前致谢。