简体   繁体   中英

Spring MVC @ResponseBody return a List

We would like to create a "WebService" which return a list of specific objects. And we would like to call this webservice from another java program by apache http clients library.

At this moment, if we call the webservice from Firefox, an 406 error page appears.

Do we have to use JSON or XML to transfert the list ? How to do that, and how to get the list with apache http clients ?

Thank you.


[EDIT]

The only thing which is working is to create some entities with JAXB annotations in order to serialize into XML.

@XmlRootElement(name = "person")
public class Person {

    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

}

@XmlRootElement(name = "persons")
public class PersonList {

    @XmlElement(required = true)
    public List<Person> persons;

    public List<Person> getData() {
        return persons;
    }

    public void setData(List<Person> persons) {
        this.persons = persons;
    }

}

@RequestMapping(value = "/hello.html", method = RequestMethod.GET, produces = "application/xml")
@ResponseBody
public ResponseEntity<PersonList> hello() {
    PersonList test = new PersonList();

    List<Person> rep = new ArrayList<Person>();
    Person person1 = new Person();
    person1.setId("1");
    Person person2 = new Person();
    person2.setId("2");

    rep.add(person1);
    rep.add(person2);

    test.setData(rep);
    // return test;

    HttpHeaders responseHeaders = new HttpHeaders();
    List<MediaType> medias = new ArrayList<MediaType>();
    medias.add(MediaType.ALL);
    responseHeaders.setAccept(medias);
    return new ResponseEntity<PersonList>(test, responseHeaders, HttpStatus.OK);
}

I tried with produces and to return directly the object but still error 406. XML + ResponseEntity works.

It is very strange cause i saw an exemple very simple where the object is converted into json and appears into web browser.

So, now i have to understand how to get the response and to convert XML into entities...

是的,当您的控制器方法使用@ResponseBody注释时,Spring @ResponseBody返回的数据转换为JSON

The @ResponseBody annotation tells Spring that we will be returning data in the response body rather than rendering a JSP.

When the @ResponseBody annotation is used, Spring will return the data in a format that is acceptable to the client. That is, if the client request has a header to accept json and Jackson-Mapper is present in the classpath, then Spring will try to serialize the return value to JSON. If the request header indicates XML as acceptable ( accept=application/xml ) and Jaxb is in the classpath and the return type is annotated with Jaxb annotation, Spring will try to marshall the return value to XML.

During 2 days, i tried many ways : - responseEntity - httpheaders - XML etc...

For a JSON (default behavior), the project need a library with all Spring library. Here the library to declare in Maven project.

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.7.1</version>
</dependency> 

Without this library, i have an Error (406).

Thank you anyway for all your answers & advices.

@ResponseBody will automatically encode the object you return to appropriate formats based on the Accept header of the request and the presence of JSON and/or XML libraries in the classpath.

It can be easier/safer to define your own object to wrap the list in though rather than returning the list directly - as that gives you more control over the encoding and also allows you to potentially add other data in the future.

You should rather use ResponseEntity for that. @ResponseBody gives you absolutely no control over the response.

You can build REST services using spring mvc framework. It will return JSON / XML. And call those services using HTTP clients / rest templates and use returned JSON to display information.

Spring controllers can return an object, list of objects as well. And some mappings (Jackson and JAXB) will allow it to convert object into JSON / XML.

If your services accept request data, you can send an object to service and get response data.

You can use Grails frameworks as well.

Actually you have to use REST web service that carry JSON/XML format as Objects representation. I prefer JSON because this is very light weight.

First you need to add dependency in your Pom.xml

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.7.1</version>

and your method handler is here

    @ResponseBody
    @RequestMapping(value = "/your URL")
    public ArrayList<Long> getInboxPage(@RequestParam int var,HttpSession session) {

        ArrayList<Long> fooList=new ArrayList<Long>();
        fooList.add(1L);
        fooList.add(2L);
        fooList.add(3L);

        return fooList;

    }

NOTE: Spring automatically make JSON if you write @ResponseBody annotation in your method handler you don't need to add Jackson dependency in your pom.xml file.

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