Reputation: 15338
@Consumes
as well as @Produces
, both accepts the Media type. Accept Header in request corresponds to which annotation?
In most of the examples (e.g. How to deal with accept-parameters when developing a jax-rs application) it shows of using @Produces
to accept the Header from client request.
Upvotes: 0
Views: 2931
Reputation: 208974
@Consumes
is for request type, i.e, the type the client is sending i.e. Content-Type
. E.g.
@Consumes("application/json")
...
> POST / HTTP/1.1
> Content-Type: application/json
{ "json": "data" }
< 200 OK
@Consumes("application/json")
...
> POST / HTTP/1.1
> Content-Type: application/xml
<xml><data></data></xml>
> 415 Unsupported Media Type
Here there are two example requests. On the server both are annotated with @Consumes("application/json")
. This means that it can only handle data being sent in JSON format. The first request, the client send the data with correct Content-Type
, so it gets an OK response. The second request, it sends XML data with a Content-Type
not supported, so it gets a 415.
The @Produces
is for the type of data sent by the server. The client can also add an Accept
header to say what type it can accept (or process). If that type is not specified in the @Produces
, then the server can't handle that type, and the client gets an error message. For example
@Produces("application/json")
...
> GET / HTTP/1.1
> Accept: application/json
< 200 OK
{ "json" : "data" }
@Produces("application/json")
...
> GET / HTTP/1.1
> Accept: application/xml
< 406 Not Acceptable
Upvotes: 2