0

I want to return XML response message after API REST call. I tried this simple test:

@RestController
public class HelloWorldRestController {

    @Autowired
    ApiService apiService;
    @RequestMapping(value = "/api", method = RequestMethod.GET)
        public ResponseEntity<VisaResponse> listAllUsers() {
            VisaResponse obj = apiService.visaresponse();
            return new ResponseEntity<VisaResponse>(obj, HttpStatus.OK);
        }
    .......
}

I tried to convert simple object to XML

public class VisaResponse {

    public VisaResponse() {
        jaxbObjectToXML(new VisaResponseHeader());
    }

    private static String jaxbObjectToXML(VisaResponseHeader customer) {
        String xmlString = "";
        try {
            JAXBContext context = JAXBContext.newInstance(VisaResponseHeader.class);
            Marshaller m = context.createMarshaller();

            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // To format XML

            StringWriter sw = new StringWriter();
            m.marshal(customer, sw);
            xmlString = sw.toString();

        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return xmlString;
    }
}

public class VisaResponseHeader {

    private int id;

    public VisaResponseHeader() {
        id = 3;
    }   
}

But when I make a rest request nothing happens - no any response. Do you have any idea where I'm wrong? Simple content should be returned.

4 Answers 4

1
  1. Add produces = "application/xml" inside the @RequestMapping. @RequestMapping(value="/api", method=RequestMethod.GET, produces=application/xml")
  2. You need to add number of JAXB annotations to your bean class (VisaResponseHeader) to allow it to be marshalled into XML.

@XmlRootElement: This annotation is used at the top level class to indicate the root element in the XML document. The name attribute in the annotation is optional. If not specified, the class name is used as the root XML element in the document.

@XmlAttribute: This annotation is used to indicate the attribute of the root element.

@XmlElement: This annotation is used on the properties of the class which will be the sub-elements of the root element.

HelloWorldRestController .java

@RestController
public class HelloWorldRestController {

    @Autowired
    ApiService apiService;

    @RequestMapping(value = "/api", method = RequestMethod.GET)
    public ResponseEntity<String> listAllUsers() {
        // get data from database
        VisaResponse visaResponse = apiService.visaresponse();

        // convert bean to XML
        String xmlResponse = jaxbObjectToXML(visaResponse);

        return new ResponseEntity<>(xmlResponse, HttpStatus.OK);
    }

    private static String jaxbObjectToXML(VisaResponse customer) {
        String xmlString = "";
        try {
            JAXBContext context = JAXBContext.newInstance(VisaResponse.class);
            Marshaller m = context.createMarshaller();

            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            StringWriter sw = new StringWriter();
            m.marshal(customer, sw);
            xmlString = sw.toString();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return xmlString;
    }
}

VisaResponse.java

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class VisaResponse {

        @XmlElement
        private int id;

        // for testing purpose. Remove once database integration is done and data is received via service and repository.
        public VisaResponse() {
            id = 3;
        }
    }

No need of VisaResponseHeader.java class.

Expected output(Tested using Postman) enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

@PeterPenzov Try modified code. This is working fine, giving expected XML output
Looks like I have wrong project setup. Can I ask you to upload your project?
I have uploaded my sample project on github.com/Dharita/Java_object_to_XML_REST_Response. You can clone it.
Thanks you very much. If you have time can you show me how I can add authentication and SSL encryption in communication?
1
@XmlRootElement
public class VisaResponseHeader {

    @XmlElement(name = "id")
    private int id;

    public VisaResponseHeader() {
        id = 3;
    }   
}

1 Comment

Unfortunately I get empty result again.
1

Try adding produces = "application/xml" inside the @RequestMapping. Your annotation should be like @RequestMapping(value = "/api", method = RequestMethod.GET,produces = "application/xml").

Along with this, you should add @XmlRootElement(name = "visa") @XmlAccessorType(XmlAccessType.FIELD) to your VisaResponse POJO and @XmlElement to each element you want in the XML.

Edited:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class VisaResponse {

    @XmlElement
    private int visaNumber;

    public int getVisaNumber() {
        return visaNumber;
    }

    public void setVisaNumber(int visaNumber) {
        this.visaNumber = visaNumber;
    }

}

Hope this helps.

2 Comments

probably you mean VisaResponseHeader class?
@PeterPenzov yes
0
 @RequestMapping(value = "/api", method = RequestMethod.GET, produces = {
        "application/xml" })
    public ResponseEntity<VisaResponse> listAllUsers() {
        VisaResponse obj = apiService.visaresponse();
        return new ResponseEntity<VisaResponse>(obj, HttpStatus.OK);
    }



    @XmlRootElement
    public class VisaResponse implements Serializable{

        private int id;

        public VisaResponse() {
            id = 3;
        } 
public getId(){
return id;
}  
public setId(int id) {
this.id = id;
}
    }

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.