简体   繁体   English

在Spring Rest中将String转换为xml响应

[英]converting String to xml Response in Spring rest

I am working on spring-boot rest application and I have a scenario where I need to send back a xml response. 我正在开发spring-boot rest应用程序,并且有一种情况需要返回xml响应。 For which I have created a JAXB class as below: 为此,我创建了一个JAXB类,如下所示:

@XmlRootElement(name = "Response")
public class ResponseDTO{

private String success;
private List<String> xmls; 

}

and my controller class is as below: 我的控制器类如下:

public class MyController{

@RequestMapping(value = "/processrequest/v1", method = RequestMethod.POST,      produces = "application/xml")
public ResponseEntity processHotelSegments(@RequestBody String xmlString) {
    ResponseDTO response = new ResponseDTO();
    response.setSuccess("true");

    String xml1 = "<triggerProcess id = '1'><code>true</code>    </triggerProcess>";
    String xml2 = "<triggerProcess id = '2'><code>false</code></triggerProcess>";
    List<String> list = new ArrayList<>();
    list.add(xml1);
    list.add(xml2);


    response.setXmls(list);
    return new ResponseEntity<>(response, HttpStatus.CREATED);

}
}

And I am expecting the xml response as below: 我期望的xml响应如下:

<Response>
    <success>true</success>
        <xmls>
            <triggerProcess id = '1'>
                <code>true</code>
            </triggerProcess>
            <triggerProcess id = '2'>
                <code>false</code>
            </triggerProcess>
        </xmls>
</Response>

ie, the String values(xml1, and xml2 should be converted to xml as well). 即,字符串值(xml1和xml2也应转换为xml)。 But I am getting the as below: 但是我得到如下:

<Response>
    <success>true</success>
        <xmls>
            <triggerProcess id = '1'><code>true</code></triggerProcess><triggerProcess id = '2'><code>false</code></triggerProcess>
        </xmls>
</Response>

where in the xmls (xml1 and xml2) are not converted to xml, instead they are displayed as the String value of elements. xml(xml1和xml2)中的位置不会转换为xml,而是显示为元素的String值。 Can anybody help me out in obtaining the output as excepted. 有人可以帮我获得例外的输出吗? Thanks in advance. 提前致谢。

You are capturing xmls as a List of Strings instead of List of Objects. 您正在将xmls捕获为字符串列表而不是对象列表。 If you wish to capture, the children of xmls as Objects then you need to define them that way in JAXB object like below. 如果希望捕获,则将xmls的子级作为对象,然后需要在JAXB对象中以如下方式定义它们。 Change your xmls to a List of TriggerProcess object that represents triggerProcess element. 改变你xmls来的名单TriggerProcess对象,表示triggerProcess元素。

@XmlRootElement(name = "Response")
public class ResponseDTO{

private String success;
private List<TriggerProcess> xmls; 

}

@XmlRootElement(name = "triggerProcess")
class TriggerProcess{
   @XmlAttribute
   private String id;
   @XmlElement
   private String code;
}

I can't see any difference between xml, that you show: 您看不到xml之间的任何区别:

First: 第一:

<Response>
    <success>true</success>
        <xmls>
            <triggerProcess id = '1'>
                <code>true</code>
            </triggerProcess>
            <triggerProcess id = '2'>
                <code>false</code>
            </triggerProcess>
        </xmls>
</Response>

Second (after formating) (格式化后)

<Response>
    <success>true</success>
        <xmls>
            <triggerProcess id = '1'>
                <code>true</code>
            </triggerProcess>
            <triggerProcess id = '2'>
               <code>false</code>
            </triggerProcess>
        </xmls>
</Response>

What your problem is? 你的问题是什么? May be, all ok? 可能吧,一切还好吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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