1

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.

4
  • I belive you might need a TriggerProcess class with code an and id members. Then you can add an list<TriggerProcess> to your RepsonseDTO class. Commented Dec 15, 2015 at 17:25
  • Thanks for your input. But the problem I am facing here is the strings xml1 and xml2 are dynamic. The elements of the xml string will vary. In that case the elements of the TriggerProcess class should also vary Commented Dec 16, 2015 at 4:26
  • When you say vary, how do they vary exactly ? You will always get triggerProcess with different elements each time or something else will come instead of triggerProcess Commented Dec 16, 2015 at 4:50
  • say for example: sometimes xml1 = "<triggerProcess id = '1'><code>true</code> </triggerProcess>" and sometimes xml1 will change to <triggerProcess id = '1'><code>true</code> <statuscode>01</statuscode> </triggerProcess>. Mean to say elements of the xml will keep on varying Commented Dec 16, 2015 at 5:15

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your input Arkantos. But the problem I am facing here is The strings xml1 and xml2 are dynamic. The elements of the xml string will vary. In that case the elements of the TriggerProcess class should also vary.
Elements of xml string vary.. In what sense do they vary ? If you're sure that there will always be triggerProcess element with 5 say elements ( all are optional) and you may get 2 children or 3 children or all 5 of them inside triggerProcess. Is this how your xml strings vary ?
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.