0

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:

  1. 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 XML
  • Accept=application/json, application/xmlthen the response is in JSON

Therefore, I am assuming the second is ignored.

If I am correct, therefore

  1. What is the point of setAccept(List<MediaType> acceptableMediaTypes)? I mean send a List?
  2. what case has sense or is useful or should work if we send a list?.
  3. What is the expected behaviour when we send a list of Accept?
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158

1 Answers1

1

The documentation says it all:

Set the list of acceptable media types

So, if you send XML and JSON as acceptable media types, and the server if capable of producing at least one of those media types, it will return a successful response. If the server is not able of producing any of those, it will return an error response.

This also answers your first question: the browser probably only sends HTML and XML as acceptable media types. Your server is not able to produce HTML, but is able to produce XML, so that's what is returned to the browser.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • About the Web Browser, I did realize when I use in my `@RequestMapping` method the parameter `@RequestHeader String accept` the `Console` (through SLF4j) shows `accept: application/xml, text/xml, application/json, application/*+xml, application/*+json`, the first of the list is `application/xml` that could answer why all the web browsers have that behaviour – Manuel Jordan Oct 28 '14 at 21:54