6

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

MychaL
  • 969
  • 4
  • 19
  • 37

7 Answers7

12

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

Lucky
  • 16,787
  • 19
  • 117
  • 151
Rytis Alekna
  • 1,387
  • 7
  • 17
11

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.

Jobin
  • 5,610
  • 5
  • 38
  • 53
Manjush
  • 494
  • 5
  • 14
4

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

Tim B
  • 40,716
  • 16
  • 83
  • 128
  • Definitely agree with comment on creating an object wrapping the list. It's the best way to control what is generated and ensure consistency across both XML and JSON results. – Steve Dec 11 '13 at 09:56
  • ok, yes it is a good idea. I only succeed an XML transfer, dunno why. Now i have to get the response... – MychaL Dec 12 '13 at 11:02
4

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.

MychaL
  • 969
  • 4
  • 19
  • 37
  • I added this to pom.xml still error: No converter found for return value of type: class java.util.ArrayList, @ResponseBody only tells Springmvc to return an object on a template name (like thymleaf template). there must be another lib I have missed but not this one – yuzhen Jun 17 '19 at 15:03
2

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

Michael-O
  • 18,123
  • 6
  • 55
  • 121
1

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.

Jeevan Patil
  • 6,029
  • 3
  • 33
  • 50
1

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.

Yasir Shabbir Choudhary
  • 2,458
  • 2
  • 27
  • 31
  • No, without jackson library, that doesn't work. It try to convert to JSON but there is HTTP error. With the library which implement JSON converter, all is fine. – MychaL Dec 02 '14 at 10:57
  • org.codehaus.jackson jackson-mapper-asl 1.7.1 I already mention the jackson maven repository – Yasir Shabbir Choudhary Dec 03 '14 at 06:04
  • So why "you don't need to add Jackson dependency in your pom.xml file" ? – MychaL Dec 03 '14 at 13:24
  • Because If you use your own custom class and want to make it JSON then we need to add Jackson Dependency in your pom.xml. otherwise if your are using built in Object in Java then your don't need for to add – Yasir Shabbir Choudhary Dec 04 '14 at 06:27