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...
Yes, when your controller method in annotated with @ResponseBody
, Spring transforms returned data into JSON.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With