因为Spring将其解释为扩展名,所以Get请求对.com电子邮件地址失败 [英] Get Request fails for .com email addresses because Spring interpretes it as a extensions
问题描述
我最近有类似问题(获取请求仅适用于尾部斜杠(Spring REST注释)),并且解决方案是向该值添加正则表达式@RequestMapping(另请参阅 Spring MVC @PathVariable被截断)。
但现在我们已经意识到问题仍然存在,但仅限于以.com或.org结尾的电子邮件地址。这很奇怪。
不久之后,我使用Spring Annotations构建了一个REST服务。我有三个参数的GET请求,最后一个是电子邮件。
我的控制台:
@RequestMapping(method = GET,value =/{value1}/{value2}/{value3:.+},
produce = MediaType.APPLICATION_JSON_VALUE +; charset = UTF-8)
public ResponseEntity< MyResponseType> getMyResource(
@ApiParam(value =...,...)
@PathVariable(value1)字符串value1,
@ApiParam(value =..., ...)
@PathVariable(value2)字符串value2,
@ApiParam(value =...,...)
@PathVariable(value3)字符串value3 ){
// ...
}
如果我打电话:
http:// myserver:8080 / myresource / value1 / value2 / value3
with value3 = email@domain.de / co.uk / .name / .biz /.info,完全没有问题。
但是,对于某些顶级域名(.com,.org到目前为止),我会收到HTTP状态码406(不接受)。
如果我添加一个尾部斜线,它就可以工作:
http:// myserver:8080 / myresource / value1 / value2 / value3 /
由于我们使用招摇和招摇不投广告d后面的斜杠,添加一个斜线不是一个选项。
什么可能导致这个问题?
I使用扩展ResponseEntityExceptionHandler的ErrorHandler。
我调试它,发现HttpMediaTypeNotAcceptableException(无法找到可接受的表示形式)被抛出。但我还是找不到是谁在投掷它以及为什么。
编辑
我发现路径 http:// myserver :8080/myresource/value1/value2/myemail@somewhere.com
被解释为文件,而com是媒体类型的文件扩展名application / x-msdownload,所以在Spring的class ProducesRequestCondition.ProduceMediaTypeExpression方法matchMediaType中,行getMediaType()。isCompatibleWith(acceptedMediaType)失败,因为实际生成的媒体类型是application / json; charset = UTF-8 ,而不是application / x-msdownload。
所以问题变为:我怎样才能让Spring明白,.com不是文件扩展名?
此线程帮助了我:
SpringMVC:取决于URL扩展的映射行为不一致
显然,这不是一个错误,而是一个功能,有两种不同的方式禁用它。我使用了注释版本:
@Configuration
@EnableWebMvc
public class RestCommonsMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer){
configurer.favorPathExtension(false);
}
}
(See edit part below 20.08.2015)
I had a similar problem recently (Get request only works with trailing slash (spring REST annotations)) and the solution was to add a regex to the value of @RequestMapping (see also Spring MVC @PathVariable getting truncated).
But now we have realised that the problem still exists, but only for email addresses ending on .com or .org. This is very weird.
Shortly, I use Spring Annotations building a REST Service. I have a GET request with three parameters, the last is an email.
My Controller:
@RequestMapping(method = GET, value = "/{value1}/{value2}/{value3:.+}",
produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
public ResponseEntity<MyResponseType> getMyResource(
@ApiParam(value = "...",...)
@PathVariable("value1") String value1,
@ApiParam(value = "...",...)
@PathVariable("value2") String value2,
@ApiParam(value = "...",...)
@PathVariable("value3") String value3) {
//...
}
If I call: http://myserver:8080/myresource/value1/value2/value3
with value3= email@domain.de / co.uk / .name / .biz /.info, there is no problem at all.
But with certain top level domains (.com, .org so far) I receive Http Status Code 406 (Not accepted).
It works if I add a trailing slash:
http://myserver:8080/myresource/value1/value2/value3/
As we use swagger and swagger does not add trailing slashes, adding a trailing slash is not an option.
What might cause this problem?
I use an ErrorHandler which extends ResponseEntityExceptionHandler. I debugged it and found that HttpMediaTypeNotAcceptableException ("Could not find acceptable representation") is thrown. But I can't find out yet who is throwing it and why.
edit
I found out that the path http://myserver:8080/myresource/value1/value2/myemail@somewhere.com
is interpreted as file, and "com" is the file extension for the media type "application/x-msdownload", so in Spring's class ProducesRequestCondition.ProduceMediaTypeExpression in the method matchMediaType the line "getMediaType().isCompatibleWith(acceptedMediaType)" fails, because the actually produced media type is "application/json;charset=UTF-8", not "application/x-msdownload".
So the questions changes to: How can I make Spring understand, that .com is not a file extension?
This thread helped me: SpringMVC: Inconsistent mapping behavior depending on url extension
Obviously this is not a bug but a "feature" and there are two different ways to disable it. I used the annotation version:
@Configuration
@EnableWebMvc
public class RestCommonsMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
}
这篇关于因为Spring将其解释为扩展名,所以Get请求对.com电子邮件地址失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!