spring 如何在jaxb生成的xml字符串中转义双引号

wfveoks0  于 2022-12-10  发布在  Spring
关注(0)|答案(2)|浏览(256)

基本上,我需要构建一个xml文件,然后将xml请求提交给restTemplate post操作,如下所示:

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

生成的xml如下:

<?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>

但是,当resttemplate提交xml时,我得到了以下错误:

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

根据错误消息,我需要转义双引号。
我怎样才能摆脱它呢?

w51jfk4q

w51jfk4q1#

尝试使用StringEscapeUtils.escapeXml(),如下所示:

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

x6yk4ghg2#

我设法通过更改以下代码来解决此问题:删除以下内容:

MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

添加了以下内容:

messageConverters.add(new StringHttpMessageConverter());

我没有将对象转换为Folder,而是使用String:

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

完整的代码如下:

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

    }

我补充这一点的情况下,如果别人有同样的问题。
谢谢

相关问题