MychaL
MychaL

Reputation: 999

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...

Upvotes: 6

Views: 59138

Answers (7)

Manjush
Manjush

Reputation: 524

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.

Upvotes: 11

Rytis Alekna
Rytis Alekna

Reputation: 1377

Yes, when your controller method in annotated with @ResponseBody, Spring transforms returned data into JSON.

Upvotes: 12

Yasir Shabbir Choudhary
Yasir Shabbir Choudhary

Reputation: 2578

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.

Upvotes: 1

MychaL
MychaL

Reputation: 999

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.

Upvotes: 4

Michael-O
Michael-O

Reputation: 18430

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

Upvotes: 2

Tim B
Tim B

Reputation: 41168

@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.

Upvotes: 4

Jeevan Patil
Jeevan Patil

Reputation: 6079

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.

Upvotes: 1

Related Questions