我的Java应用程序中有jersey REST客户端,我想以JSON格式发送请求,并且请求对象包含Element.
当我以XML格式发送请求时,一切正常,但是当尝试以JSON格式发送请求时,服务器发送请求失败.
如果我创建元素而不附加到文档,则会产生以下错误:
com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle
如果我创建元素并将其附加到文档,则会生成以下错误(doc.appendChild(root);):
com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (*Error)
这是我创建请求对象的方式:
public OTAAirLowFareSearchRS AirLowFareSearch(OTAAirLowFareSearchRQ request) throws Exception {
OTAAirLowFareSearchRS response = new OTAAirLowFareSearchRS();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.newDocument();
Element root = doc.createElement("TPA_Extension");
doc.appendChild(root); // This the line I mention above that create different errors
Element e = doc.createElement("CUSTOM_VAR");
root.appendChild(e);
e.insertBefore(doc.createTextNode("CUSTOM_VALUE"), e.getLastChild());
TPAExtensionsType tpa = new TPAExtensionsType();
tpa.getAny().add(root);
request.getPOS().getSource().get(0).setTPAExtensions(tpa);
JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
Client client = ClientBuilder.newClient(new ClientConfig(jacksonJsonProvider));
WebTarget target = client.target(GlobalVariable.bus_url).path("AirLowFareSearch");
response = target
.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(request, MediaType.APPLICATION_JSON_TYPE),
OTAAirLowFareSearchRS.class);
return response;
}
当我以XML格式发送请求时,一切正常:
//JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//Client client = ClientBuilder.newClient(new ClientConfig(jacksonJsonProvider));
Client client = ClientBuilder.newClient();
WebTarget target = client.target(GlobalVariable.bus_url).path("AirLowFareSearch");
response = target
.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(request, MediaType.APPLICATION_XML_TYPE),
OTAAirLowFareSearchRS.class);
当我发送请求而不添加元素时,我也可以发送JSON请求(因此在配置JSON时没有错误).
这也是“打开旅行”对象,我也无法更改请求.
请帮我.
解决方法:
问题来自DOM Elements.解决此问题的最佳方法是使用地图而不是DOM元素,以便您轻松处理地图.