Reputation: 731
I am working on spring-boot rest application and I have a scenario where I need to send back a xml response. For which I have created a JAXB class as below:
@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:
<Response>
<success>true</success>
<xmls>
<triggerProcess id = '1'>
<code>true</code>
</triggerProcess>
<triggerProcess id = '2'>
<code>false</code>
</triggerProcess>
</xmls>
</Response>
i.e, the String values(xml1, and xml2 should be converted to xml as well). 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. Can anybody help me out in obtaining the output as excepted. Thanks in advance.
Upvotes: 1
Views: 4134
Reputation: 6608
You are capturing xmls
as a List of Strings instead of List of Objects. If you wish to capture, the children of xmls
as Objects then you need to define them that way in JAXB object like below. Change your xmls
to a List of TriggerProcess
object that represents triggerProcess
element.
@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;
}
Upvotes: 1
Reputation: 60094
I can't see any difference between xml, that you show:
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?
Upvotes: 0