I try to access an open data web service which gives me traffic infos. Documentation says that requests have to be GET and need to contain Accept: applica
Isn't
accept(MediaType.APPLICATION_JSON)andheader("Content-type","application/json")the same?
No, they are different.
This is how they are related:
Client Server
(header) (class/method annotation)
====================================================
Accept <---> @Produces
Content-Type <---> @Consumes
The server consumes what it receives from the client in body (its format is specified in Content-Type) and it produces what the client accepts (its format is specified in Accept).
Example:
Content-Type = text/xml (it sends an XML in body)Accept = application/json (it expects to get a JSON as a response)@Consumes(MediaType.TEXT_XML) (it gets an XML from the client)@Produces(MediaType.APPLICATION_JSON) (it sends a JSON to the client)Obs:
The server can be more flexible, being configured to get/produce multiple possible formats.
E.g.: a client can send an XML while another client can send a JSON to the same method if it has the following annotation: @Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML }).
The MediaType values are just String constants:
public final static String APPLICATION_JSON = "application/json";
public final static String TEXT_XML = "text/xml";