将java对象转换为XML REST响应

时间:2018-05-30 10:05:44

标签: java rest jaxb spring-rest

我想在API REST调用后返回XML响应消息。我尝试了这个简单的测试:

@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);
        }
    .......
}

我尝试将简单对象转换为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;
    }   
}

但是当我提出休息请求时没有任何反应 - 没有任何回应。你知道我哪里错了吗?应该返回简单的内容。

4 个答案:

答案 0 :(得分:2)

  1. 在@RequestMapping中添加 produce =&#34; application / xml&#34; @RequestMapping(value="/api", method=RequestMethod.GET, produces=application/xml")
  2. 您需要在bean类(VisaResponseHeader)中添加 JAXB注释 的数量,以便将其编组为XML。
  3. @XmlRootElement:此注释用于顶级类,以指示XML文档中的根元素。注释中的name属性是可选的。如果未指定,则类名将用作文档中的根XML元素。

    @XmlAttribute:此注释用于指示根元素的属性。

    @XmlElement:此注释用于类的属性,该属性将是根元素的子元素。

    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;
            }
        }
    

    不需要VisaResponseHeader.java类。

    预期输出(使用邮递员测试) enter image description here

答案 1 :(得分:1)

@XmlRootElement
public class VisaResponseHeader {

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

    public VisaResponseHeader() {
        id = 3;
    }   
}

答案 2 :(得分:1)

尝试在produces = "application/xml"内添加@RequestMapping。 你的注释应该是这样的  @RequestMapping(value = "/api", method = RequestMethod.GET,produces = "application/xml")

除此之外,您应该将@XmlRootElement(name = "visa") @XmlAccessorType(XmlAccessType.FIELD)添加到VisaResponse POJO,并@XmlElement添加到XML中所需的每个元素。

编辑:

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;
    }

}

希望这有帮助。

答案 3 :(得分: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;
}
    }