简体   繁体   中英

Spring 3.0 + REST web service + json + xml

I have tried a lot of things now and looked up many places for this problem.

I am trying to use Spring MVC 3.0 to make a Rest web service call to get data in the form of JSON and XML. At the moment from what I have understood, when the jackson core and mapper jars are present in the classpath, Spring will automatically convert the response to jSon which is fine. This works as expected in the project.

What i am struggling with is to generate an xml response. I am not sure what I have to do for this. I have tried various ways to set the method to produce an xml response. As per my understanding jdk 6 and above have JAXB implementations. So when I annotate my POJO with XML annotations to produce XML, it should give me an XML response. But for some reason it keeps giving me JSON reponse. Any ideas? Following is my POJO which has the XMLRootElement jaxb annotation.

    package my.dto;

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

    @XmlRootElement(name = "mydto")
    public class MyDTO {

        String name;
        Long quantity;
        String type;


        public String getName() {
            return name;
        }
        @XmlElement
        public void setName(String name) {
            this.name = name;
        }


        public String getType() {
            return type;
        }
        @XmlElement
        public void setType(String type) {
            this.type = type;
        }


        public Long getQuantity() {
            return quantity;
        }
        @XmlElement
        public void setQuantity(Long quantity) {
            this.quantity = quantity;
        }

        public MyDTO(String name, Long quantity) {
            this.name = name;
            this.quantity = quantity;
        }

        public MyDTO() {
            super();
        }

    }

**My Controller:**

package my.package;

import java.util.ArrayList;
import java.util.List;

import my.dto.MyDTO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Controller
@RequestMapping("/spring")
public class MyController {



    @RequestMapping(value="{b}", method = RequestMethod.GET)
    @Produces("application/xml")
    public @ResponseBody List<MyDTO> getMyList(@PathVariable String b) {


        MyDTO m1 = new MyDTO("My 1", Long.valueOf(100));
        MyDTO m2 = new MyDTO("My 2", Long.valueOf(200));

        List<MyDTO> list = new ArrayList<MyDTO>();
        list.add(m1);
        list.add(m2);

        return list;
    }

}

One:

Yes , you need the @XML annotations but they must be located in the getters , you have defined on the setters, that is wrong.

Second

You are returning a collection, for XML it is a little different, you need create the following

@XmlRootElement(name="list")
public class JaxbMyDTOList {

    private List<MyDTO> list;

    public JaxbMyDTOList(){}

    public JaxbMyDTOList(List<MyDTO> list){
        this.list=list;
    }

    @XmlElement(name="item")
    public List<MyDTO> getList(){
        return list;
    }
}

Three:

Change from:

@RequestMapping(value="{b}", method = RequestMethod.GET)
@Produces("application/xml")
public @ResponseBody List<MyDTO> getMyList(@PathVariable String b) {

To

@RequestMapping(value="{b}", 
                method = RequestMethod.GET, 
                produces=MediaType.APPLICATION_XML_VALUE)
public @ResponseBody JaxbMyDTOList getMyList(@PathVariable String b) {

Observe:

  • @Produces("application/xml") removed and replaced by produces=MediaType.APPLICATION_XML_VALUE)
  • List<MyDTO> replaced by JaxbMyDTOList

Four:

I suggest you create other @RequestMapping method, one for JSON and other to handle XML

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM