简体   繁体   English

标头参数如何用于RequestMapping注释?

[英]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"} 但是,当我添加@RequestMapping参数headers = {"Content-Disposition=attachment", "filename=mockDump.csv"}

method doesn't invoke. 方法不会调用。

Why? 为什么?

The @RequestMapping header describes headers which must be present on the request for it to be routed to the annotated method for handling. @RequestMapping标头描述了标头,该标头必须存在于请求中,才能将其路由到带注释的方法进行处理。 Where as calling setHeader on a response object sets a header to be sent to the client in the repsonse 在响应对象上调用setHeader时,设置要在响应中发送给客户端的标头

As the @RequestMapping#headers javadoc states @RequestMapping#headers javadoc所述

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. 换句话说, headers适用于请求标头,有助于确定适当的处理程序方法。

If you want the Spring way of adding response headers, ie. 如果您想通过Spring方式添加响应头,即。 not through the HttpServletResponse , return a ResponseEntity with an appropriate HttpHeaders set. 而不通过HttpServletResponse ,返回一个具有适当HttpHeaders设置的ResponseEntity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM