2016-12-07 83 views
3

包含DOM元素JSON格式的請求在我java應用jerseyREST客戶端,我想在JSON格式發送的請求,並請求object包含Element我如何發送使用的球衣REST客戶

當我發送請求與XML格式一切工作正常,但當試圖發送請求與JSON格式服務器未能發送請求。

如果我不追加到Document創建Element它產生以下錯誤:

com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle 

如果我創建Element並將其追加到一個Document它產生以下錯誤(doc.appendChild(root);):

com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) 

這是我創建我的請求的方式對象:

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); 

當我送不增加Element我的要求,我可以送JSON要求太(所以沒有錯誤配置JSON)。

這也是Open Travel對象,我也無法改變請求。

請幫幫我。

回答

3

問題來自DOM Elements。解決此問題的最佳方法是使用地圖而不是DOM元素,以便您可以輕鬆地處理地圖。

+2

優秀的解決方案。 –

相關問題