gstackoverflow
gstackoverflow

Reputation: 37004

How does headers argument works for RequestMapping annotation?

When I write inside controller method:

response.setHeader("Content-Disposition", "attachment; filename=mockDump.csv");

It works good.

But when I adds in @RequestMapping argument headers = {"Content-Disposition=attachment", "filename=mockDump.csv"}

method doesn't invoke.

Why?

Upvotes: 0

Views: 628

Answers (2)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280172

As the @RequestMapping#headers javadoc states

The headers of the mapped request, narrowing the primary mapping.

In other words, headers applies to the request headers, helping to determine the appropriate handler method.

If you want the Spring way of adding response headers, ie. not through the HttpServletResponse, return a ResponseEntity with an appropriate HttpHeaders set.

Upvotes: 1

beresfordt
beresfordt

Reputation: 5222

The @RequestMapping header describes headers which must be present on the request for it to be routed to the annotated method for handling. Where as calling setHeader on a response object sets a header to be sent to the client in the repsonse

Upvotes: 1

Related Questions