I am working with Spring 4.0.7
I have an entity where it is represented in JSON and XML.
@Entity
@Table(name="person")
@XmlRootElement(name="person")
@XmlType(propOrder = {"id",…,"address"})
public class Person implements Serializable {
I have the following method:
@RequestMapping(value="/{id}/customized",
method=RequestMethod.GET,
produces={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
public ResponseEntity<Person> getPersonCustomized(@PathVariable Integer id){
logger.info("PersonRestResponseEntityController - getPersonCustomized - id: {}", id);
Person person = personMapRepository.findPerson(id);
return new ResponseEntity<>(person, HttpStatus.FOUND);//302
}
Through RestTemplate I can do the following:
public Person getPersonCustomized(String id, String type){
HttpHeaders headers = new HttpHeaders();
if(type.equals("JSON")){
logger.info("JSON");
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
}
else if(type.equals("XML")){
logger.info("XML");
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
}
else if(type.equals("MIX01")){
logger.info("MIX01 - XML _ JSON");
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON));
}
else if(type.equals("MIX02")){
logger.info("MIX01 - JSON _ XML");
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML));
}
ResponseEntity<Person> response =
restTemplate.exchange("http://localhost:8080/spring-utility/person/{id}/customized",
HttpMethod.GET,
new HttpEntity<Person>(headers),
Person.class,
id
);
logger.info("status: {}", response.getStatusCode());
logger.info("body: {}", response.getBody());
return response.getBody();
}
The app works fine. No exceptions etc..
But I am confused about the following:
One
If I put in any web browser the URL:
http://localhost:8080/spring-utility/person/2/customized
Automatically it renders the content how is expected but always in XML.
Even when I have:
produces={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
I have assumed JSON should be selected first (seems the order does not matters)
Anyway, always XML is chosen how default.
Therefore:
- Why XML has been chosen how the default format?
Two
According with the API HttpHeaders for the method setAccept
setAccept
public void setAccept(List<MediaType> acceptableMediaTypes)
Set the list of acceptable media types, as specified by the Accept header.
Parameters:
acceptableMediaTypes - the acceptable media types
It has a List<MediaType>
, so I have decided to test and play.
How you can see through my code shown above, I have two scenarios
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON));
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML));
Where each one represent
Accept=application/xml, application/json
Accept=application/json, application/xml
Well through the RestTemplate I can see always the Person object already transformed or converted, so there is no nothing new
Therefore, just playing with Poster
If I set the headers value
Accept=application/xml, application/json
then the response is in XMLAccept=application/json, application/xml
then the response is in JSON
Therefore, I am assuming the second is ignored.
If I am correct, therefore
- What is the point of
setAccept(List<MediaType> acceptableMediaTypes)
? I mean send aList
? - what case has sense or is useful or should work if we send a list?.
- What is the expected behaviour when we send a list of Accept?