我正在开发 spring-boot rest 应用程序,并且我有一个需要发回 xml 响应的场景。为此,我创建了一个 JAXB 类,如下所示:
@XmlRootElement(name = "Response")
public class ResponseDTO{
private String success;
private List<String> xmls;
}
我的控制器类如下:
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);
}
}
我期待 xml 响应如下:
<Response>
<success>true</success>
<xmls>
<triggerProcess id = '1'>
<code>true</code>
</triggerProcess>
<triggerProcess id = '2'>
<code>false</code>
</triggerProcess>
</xmls>
</Response>
即,字符串值(xml1 和 xml2 也应转换为 xml)。但我得到如下:
<Response>
<success>true</success>
<xmls>
<triggerProcess id = '1'><code>true</code></triggerProcess><triggerProcess id = '2'><code>false</code></triggerProcess>
</xmls>
</Response>
其中 xml(xml1 和 xml2)不会转换为 xml,而是显示为元素的字符串值。任何人都可以帮助我获得例外的输出。提前致谢。