当路径变量包含 (.) 时,@ResponseBody 不起作用
when path variable contains a (.) then @ResponseBody is not working
我将电子邮件 ID 作为路径变量发送到服务器:
@RequestMapping(value = "/resetPassword/{email:.+}", method = RequestMethod.GET)
public @ResponseBody MyResponse resetPassword(HttpServletRequest request, @PathVariable("email") String email)
{
MyResponse res = new MyResponse();
res.setMsg("some Text");
return res;
}
我通过 jQuery 调用该方法为:
var email = $("#fpusername").val();
$.ajax({
type : "GET",
url : "./useraccount/resetPassword/" + email,
dataType : "json",
async : true,
success : function(data) {
alert(data.msg);
}
});
当我将 myname@gmail
作为 email
值发送时,同样的方法有效,但在发送 myname@gmail.com
时出现以下错误
406 [The resource identified by this request is only capable
of generating responses with characteristics not acceptable according to the request "accept" headers]
您是否在 web.xml 中映射了 url 模式?
如果不是,您必须以这种方式映射 url 以接受您的 url 通过 spring:
有效
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.com</url-pattern>
</servlet-mapping>
doc 使用等效示例警告此陷阱
By default Spring MVC automatically performs ".*"
suffix pattern
matching so that a controller mapped to /person
is also implicitly
mapped to /person.*
. This allows indicating content types via file
extensions, e.g. /person.pdf
, /person.xml
, etc. A common pitfall
however is when the last path segment of the mapping is a URI
variable, e.g. /person/{id}
. While a request for /person/1.json
would
correctly result in path variable id=1
and extension ".json"
, when the
id naturally contains a dot, e.g. /person/joe@email.com
the result
does not match expectations. Clearly here ".com"
is not a file
extension.
在决定响应的内容类型时,Spring 使用所谓的 PPA 策略(路径、参数、接受 header)。在这里,您的 .com
被视为路径(扩展名),并尝试解析基于它的表示,因此您的例外。你可以采取两条路来解决这个问题。
要么将 Spring 配置为仅使用已注册的后缀,例如在 XML
<mvc:annotation-driven>
<mvc:path-matching registered-suffixes-only="true" />
</mvc:annotation-driven>
文档和等效的 java 配置可用 here
或者,如果这对您来说是可行的解决方案,请关闭路径匹配,例如
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="mediaTypes" >
<value>
json=application/json
xml=application/xml
</value>
</property>
</bean>
文档和等效的 java 配置可用 here
我将电子邮件 ID 作为路径变量发送到服务器:
@RequestMapping(value = "/resetPassword/{email:.+}", method = RequestMethod.GET)
public @ResponseBody MyResponse resetPassword(HttpServletRequest request, @PathVariable("email") String email)
{
MyResponse res = new MyResponse();
res.setMsg("some Text");
return res;
}
我通过 jQuery 调用该方法为:
var email = $("#fpusername").val();
$.ajax({
type : "GET",
url : "./useraccount/resetPassword/" + email,
dataType : "json",
async : true,
success : function(data) {
alert(data.msg);
}
});
当我将 myname@gmail
作为 email
值发送时,同样的方法有效,但在发送 myname@gmail.com
406 [The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers]
您是否在 web.xml 中映射了 url 模式? 如果不是,您必须以这种方式映射 url 以接受您的 url 通过 spring:
有效<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.com</url-pattern>
</servlet-mapping>
doc 使用等效示例警告此陷阱
By default Spring MVC automatically performs
".*"
suffix pattern matching so that a controller mapped to/person
is also implicitly mapped to/person.*
. This allows indicating content types via file extensions, e.g./person.pdf
,/person.xml
, etc. A common pitfall however is when the last path segment of the mapping is a URI variable, e.g./person/{id}
. While a request for/person/1.json
would correctly result in path variableid=1
and extension".json"
, when the id naturally contains a dot, e.g./person/joe@email.com
the result does not match expectations. Clearly here".com"
is not a file extension.
在决定响应的内容类型时,Spring 使用所谓的 PPA 策略(路径、参数、接受 header)。在这里,您的 .com
被视为路径(扩展名),并尝试解析基于它的表示,因此您的例外。你可以采取两条路来解决这个问题。
要么将 Spring 配置为仅使用已注册的后缀,例如在 XML
<mvc:annotation-driven>
<mvc:path-matching registered-suffixes-only="true" />
</mvc:annotation-driven>
文档和等效的 java 配置可用 here
或者,如果这对您来说是可行的解决方案,请关闭路径匹配,例如
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="mediaTypes" >
<value>
json=application/json
xml=application/xml
</value>
</property>
</bean>
文档和等效的 java 配置可用 here