[英]How to specify a fallback request mapping in Spring Boot
We have an endpoint which can return different responses depending on the Accept header. 我们有一个端点,可以根据Accept标头返回不同的响应。 In particular, it can return zip files, video files, or audio files.
特别是,它可以返回zip文件,视频文件或音频文件。
Mapping 1: 映射1:
@RequestMapping(value = "endpoint",
method = RequestMethod.GET,
produces = {"video/*", "audio/*"})
Mappping 2: Mappping 2:
@RequestMapping(value = "endpoint",
method = RequestMethod.GET, produces = {"application/zip", "*/*"})
This setup will take an Accept: video/*
and go to mapping 1 (which is what we want). 此设置将采用
Accept: video/*
并转到映射1(这是我们想要的)。 But, Accept: video/mp4
results in an java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path
exception. 但是,
Accept: video/mp4
导致java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path
异常java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path
。
I would have expected that video/mp4
more closely matches mapping 1 and loads that. 我
video/mp4
期望video/mp4
更接近匹配映射1并加载它。 Indeed that is exactly what we do want. 确实,这正是我们想要的。
We can remove the */*
and then Accept: video/mp4
does go to mapping 1. However, we do need the */*
to go to Mapping 2. 我们可以删除
*/*
然后Accept: video/mp4
确实转到映射1.但是,我们确实需要*/*
去映射2。
Why doesn't Accept: video/mp4
match Mapping 1 since this is a closer match? 为什么不
Accept: video/mp4
匹配映射1,因为这是一个更接近的匹配?
Can we configure this endpoint to have a default method if no other accept header more closely matches? 如果没有其他接受标头更接近匹配,我们可以将此端点配置为具有默认方法吗? Then we could have mapping 1 only declare that it produces
application/zip
. 然后我们可以让映射1声明它产生
application/zip
。
We are using Spring Boot 1.5.3. 我们正在使用Spring Boot 1.5.3。
Why not remove produces
all together and generate the Content-Type
within the method and also have an if condition to test the Accepts
request header? 为什么不一起删除
produces
并在方法中生成Content-Type
,并且还有一个if条件来测试Accepts
请求标头? You could call two different methods depending on the request header. 您可以根据请求标头调用两种不同的方法。
@RequestMapping(value = "endpoint", method = RequestMethod.GET)
public void doIt(HttpServletRequest request, HttpServletResponse response) {
if (request.getHeader("Accept").matches("application/zip"))
doZip();
else
doVideo();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.