What priority order for MIME types (contain in accept header from request) consider by Jersey while selecting the appropriate restful endpoint

StackOverflow https://stackoverflow.com/questions/22998139

Вопрос

Please refer to the below code snippet

@Path("/hello")

public class Hello {

  @GET
  @Produces(MediaType.APPLICATION_XHTML_XML)
  public String sayApplicationXhtmlXml() {
    System.out.println("Application XHTML XML IS ACCEPTED");  
    return "Hello Jersey";
  }


  @GET
  @Produces("*/*")
  public String sayAllMimeTypes() {
    System.out.println("All MIME TYPES IS ACCEPTED");
    return "<?xml version=\"1.0\"?>" + "<hello> Hello " + "</hello>";
  }
}

When I am sending request from firefox with header

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,asterisk/asterisk;q=0.8

which is default header of firefox.

output is

ALL MIME TYPES IS ACCEPTED

I think something wrong it.

As per my understanding, output should be

Application XHTML XML IS ACCEPTED 

because application/xhtml+xml appears before "asterisk/asterisk" in Accept header (As per rule according to which if default value of q=1.0 for both of them so whoever appears first should be given higher priority)

Это было полезно?

Решение

In your request header, you specify that both text/html and application/xhtml+xml have equal quality factor (default to 1). They both have higher q value than the */* wildcard type. However for Jersey, when two MIME types have equal q values, then it will choose the MIME type according to the order they appear. So in this case, text/html will be chosen and since your second method sayAllMimeTypes() produces any MIME types including text/html. Therefore the second method is invoked.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top