J.P
J.P

Reputation: 141

Accept header in REST

I always use produces and consumes in @RequestMapping.

I know found example where in post request , @RequestMapping have headers = "Accept=application/json".So my question what this really doing? and do we need that?

Upvotes: 2

Views: 4421

Answers (4)

Fahad Sheikh
Fahad Sheikh

Reputation: 26

In REST, certain information is conveyed through header eg. session id, params, etc. the keywords like Accept, content-type, restrict the content of the header to be in specific format. The Accept request-header field can be used to specify certain media types which are acceptable for the response. Accept headers can be used to indicate that the request is specifically limited to a small set of desired types such as xml, json, media types, etc. RequestMapping annotation can be used to handle dynamic URIs where one or more of the URI value works as a parameter. Hope it clears the doubt, if not please contact...

Upvotes: 0

Danio
Danio

Reputation: 1114

This header indicates what data format client expects to receive. Setting expected header to some value in headers (e.g. application/json) in your endpoint is semantically the same as setting it in produces attribute, but produces is smarter, e.g. if you can produce multiple data types (like XML and JSON), Spring can automatically choose the right Producer to generate response relevant to the received value.

Upvotes: 0

The produces and consumes properties were not available on the first versions of @RequestMapping (they were added in Spring 3.1, I believe). Explicitly specifying the Accept header was the approach required before Spring added specific support for them.

Upvotes: 0

rorschach
rorschach

Reputation: 2947

Produces and Consumes declares what kind of data your backend method.. produces and consumes. Accept is a client-side header which tells the server what type of content you want back.

Upvotes: 1

Related Questions