Home > Software design >  how to escape double quote in xml String generated by jaxb
how to escape double quote in xml String generated by jaxb

Time:12-06

Basically I need to build an xml file and then submit the xml request to a restTemplate post action as follows:

final RestTemplate restTemplate1 = new RestTemplate();
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_XML));
        messageConverters.add(converter);
        restTemplate1.setMessageConverters(messageConverters);
    HttpHeaders headers = new HttpHeaders();
    headers.setBasicAuth("okmAdmin", "admin");
    headers.setContentType(MediaType.APPLICATION_XML);


    JAXBContext context = null;
    context = JAXBContext.newInstance(Folder.class);
    Marshaller mar = null;
    mar = context.createMarshaller();
    mar.setProperty("jaxb.encoding", "Unicode");
    mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);

    //Print XML String to Console
    StringWriter sw = new StringWriter();

    Folder folder = new Folder();
    folder.setPath("/okm:root/test4");

    //Write XML to StringWriter
    mar.marshal(folder, sw);

    String xmlContent = sw.toString();

    System.out.println(xmlContent);

    HttpEntity<String> request = new HttpEntity<String>(xmlContent, headers);

    ResponseEntity<Folder> response = restTemplate1.postForEntity("http://localhost:8080/OpenKM/services/rest/folder/create", request, Folder.class);

The xml generated is as follows:

<?xml version="1.0" encoding="Unicode" standalone="yes"?><folder><path>/okm:root/test4</path><permissions>0</permissions><subscribed>false</subscribed><hasChildren>false</hasChildren><style>0</style></folder>

However when the xml is submitted by the resttemplate,I got the following errors:

2022-12-05 16:50:21.121 ERROR 15764 --- [nio-8082-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException$BadRequest: 400 : "JAXBException occurred : Unexpected character '"' (code 34) in prolog; expected '<'<EOL> at [row,col {unknown-source}]: [1,1]. "] with root cause

Based on the error message it looks like i need to escape the double quote.

How can I escape it pls?

CodePudding user response:

Try using StringEscapeUtils.escapeXml() like the following :

HttpEntity<String> request = new HttpEntity<String>(StringEscapeUtils.escapeXml(xmlContent), headers);

CodePudding user response:

I manage to fix the issue by changing the following code: remove this:

MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

Added this:

 messageConverters.add(new StringHttpMessageConverter());

And instead of casting the object to Folder I have used String:

     ResponseEntity<String> response = restTemplate1.postForEntity("http://localhost:8080/OpenKM/services/rest/folder/create", request, String.class);

The complete code is as follows:

final RestTemplate restTemplate1 = new RestTemplate();
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        messageConverters.add(new StringHttpMessageConverter());

    restTemplate1.setMessageConverters(messageConverters);

        HttpHeaders headers = new HttpHeaders();
        headers.setBasicAuth("okmAdmin", "admin");
        headers.setContentType(MediaType.APPLICATION_XML);


        JAXBContext context = null;
        context = JAXBContext.newInstance(Folder.class);
        Marshaller mar = null;
        mar = context.createMarshaller();

        mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);

        //Print XML String to Console
        StringWriter sw = new StringWriter();

        Folder folder = new Folder();
        folder.setPath("/okm:root/test23");

        //Write XML to StringWriter
        mar.marshal(folder, sw);


        HttpEntity<String> request = new HttpEntity<String>(xmlString, headers);


        ResponseEntity<String> response = restTemplate1.postForEntity("http://localhost:8080/OpenKM/services/rest/folder/create", request, String.class);

        System.out.println(response);


    }

I am adding this in case if someone else have the same issue.

Thanks

  • Related