简体   繁体   中英

how to send a request that contains dom Element in JSON format using jersey REST client

I have jersey REST client in my java application, I want to send a request in JSON format, and the request object contains Element .

When I send request with XML format everything works fine, but when try to send request with JSON format server failed to send a request.

If I create Element without appending to a Document it generate following error:

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

If I create Element and append it to a Document it generate following error( doc.appendChild(root); ):

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

this is the way I create my request Object:

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

When I send my request with XML format like below everything works:

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

When I send my request without adding Element I can send JSON request too (so there is no error in configuring JSON ).

Also this is Open Travel object and I can't change the request too.

Please help me.

the problem comes from DOM Elements. the best way to solve it is to use map instead of DOM elements so you can deal with Maps easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM