gpt4 book ai didi

jquery - 当路径变量包含 (.) 时,@ResponseBody 不起作用

转载 作者:行者123 更新时间:2023-12-01 01:23:05 24 4
gpt4 key购买 nike

我将电子邮件 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]

最佳答案

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

关于jquery - 当路径变量包含 (.) 时,@ResponseBody 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30013736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com