忽略 spring mvc 中的 Accept 标头
我有一个提供文件(图像、pdf 等)的控制器:
@Controller
public class FileController {
@ResponseBody
@RequestMapping("/{filename}")
public Object download(@PathVariable String filename) throws Exception {
returns MyFile.findFile(filename);
}
}
如果我请求具有以下 Accept 标头的文件,我会收到 406:
<前><代码>请求 网址:http://localhost:8080/files/thmb_AA039258_204255d0.png 请求方式:GET 状态代码:406 不可接受 请求标头 接受:*/*
如果我使用以下 Accept 标头请求相同的文件,我会得到 200:
URL:http://localhost:8080/files/thmb_AA039258_204255d0.png 请求方式:GET 状态码:200 正常 请求标头 接受:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
这是我的 spring-mvc 上下文中唯一的视图解析器:
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>
是否有配置 spring mvc 来忽略 Accept 标头?我见过使用 ContentNegotiatingViewResolver 执行此操作的示例,但仅用于处理 xml 和 json。
I have a controller that serves files (images, pdfs, etc,.):
@Controller
public class FileController {
@ResponseBody
@RequestMapping("/{filename}")
public Object download(@PathVariable String filename) throws Exception {
returns MyFile.findFile(filename);
}
}
If I request a file with the following Accept header I get a 406:
Request URL: http://localhost:8080/files/thmb_AA039258_204255d0.png Request Method:GET Status Code:406 Not Acceptable Request Headers Accept:*/*
If I request the same file with the following Accept header I get a 200:
URL: http://localhost:8080/files/thmb_AA039258_204255d0.png Request Method: GET Status Code:200 OK Request Headers Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
This is the only view resolver in my spring-mvc context:
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>
Is there anyway to configure spring mvc to ignore the Accept header? I've seen example of doing this with ContentNegotiatingViewResolver, but only for handling xml and json.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以这是我最终让它工作的代码:
So this is the code I ended up with to get it working:
我用它来锁定 JSON 响应类型:
需要
favorPathExtension(false)
,因为 Spring 默认情况下(至少在 4.1.5 中)支持基于路径的内容协商(即,如果 URL 以“结尾” .xml”,它将尝试返回 XML 等)。I used this to lock to the JSON response type:
favorPathExtension(false)
is needed because Spring by default (at least in 4.1.5) favors path-based content negotiation (i.e. if the URL ends with ".xml", it will try to return XML, etc.).当您使用 ResponseBody 注释时,我认为这是处理的一部分,它查看 Accept 标头并尝试进行一些映射或其他操作。如果您不知道如何使用该注释来发送响应,还有很多其他方法可以发送响应。
When you use ResponseBody annotation, I think that is part of the deal that it looks at the Accept header and tries to do some mapping or whatever. There are plenty of other ways to send a response though if you can't figure out how to do it with that annotation.