Reputation: 3325
for class exposing resource "/hello":
@Path("/hello")
public class Test {
//-- produces MIME type text/plain
@GET
@Produces(MediaType.TEXT_PLAIN)
public String thankYouTxt(){
System.out.println("thankYouTxt");
return "thankYouTxt\n";
}
//-- consumes MIME type text/plain
@GET
@Consumes(MediaType.TEXT_PLAIN)
public String thankYouInputTxt(){
System.out.println("thankYouInputTxt");
return "thankYouInputTxt";
}
//-- produces MIME type text/html
@GET
@Produces(MediaType.TEXT_HTML)
public String thankYouHTML(){
System.out.println("thankYouHTML");
return "thankYouTxtHTML";
}
//-- consumes MIME type text/html
@GET
@Consumes(MediaType.TEXT_HTML)
public void thankYouInputHTML(){
System.out.println("thankYouInputHTML");
//return "thankYouInputHTML";
}
//-- produces MIME type text/xml
@GET
@Produces(MediaType.TEXT_XML)
public String thankYouXML(){
System.out.println("thankYouXml");
return "<?xml version=\"1.0\"?> <message>thankYouTxt</message>";
}
//-- consumes MIME type text/xml
@GET
@Consumes(MediaType.TEXT_XML)
public String thankYouInputXML(){
System.out.println("thankYouInputXML");
return "thankYouInputXML";
}
}
on the Api docu they state about the MIME acceptance order:
If a resource class is capable of producing more that one MIME media type, the resource method chosen will correspond to the most acceptable media type as declared by the client. More specifically, the Accept header of the HTTP request declared what is most acceptable. For example if the Accept header is Accept: text/plain, the doGetAsPlainText method will be invoked. Alternatively if the Accept header is Accept: text/plain;q=0.9, text/html, which declares that the client can accept media types of text/plain and text/html, but prefers the latter, then the doGetAsHtml method will be invoked.
who/what decides which MIME type (text/html) to consume rather than another one (text/html) ?
Upvotes: 0
Views: 910
Reputation: 121860
OK, rephrasing the documentation since there seems to be a misunderstanding:
the resource method chosen will correspond to the most acceptable media type as declared by the client
and then:
the Accept header of the HTTP request declare[s] what is most acceptable
This header has the following form:
Accept: media/type1[;q=pref], media/type2[;q=pref], etc etc
When q
is not mentioned it is 1.0.
If you have a request with methods producing, say, foo/bar
and tar/feathers
, if the accept header declares an acceptance of 1 for the first but 0 for the second then the method producing foo/bar
will run. If, on the other hand the first has acceptance 0.5 but the second has acceptance 0.6, then... You get the picture.
As to what happens if no type is really preferred, the doc does not even say. But then the client should decide itself in the first place.
Upvotes: 1