微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 解码@PathVariable的地方和方法

客户端在服务器上发送(实现无关紧要):

/path/items/ + urlencode(id,SOME_ENCODING)

考虑结果URL将是:

/path/items/my%2Fkey

因此我在服务器上:

@RequestMapping(value = "/path/items/{identifier}",method = RequestMethod.GET)
public Item get(@PathVariable String identifier) {
try {
    return DAO.getItemByIdentifier(URLDecoder.decode(identifier,SOME_ENCODING))
}
catch (UnsupportedEncodingException e) {
...
}

在内部有没有办法在Spring中做到这一点?我的意思是标识符已经解码,所以我可以:

@RequestMapping(value = "/path/items/{identifier}",method = RequestMethod.GET)
    public Item get(@PathVariable String identifier) {
return DAO.getitemByidentifier(identifier); // already decoded!
    }
最佳答案
您应该在server.xml中的tomcat连接器中设置URIEncoding.

看看:http://tomcat.apache.org/tomcat-5.5-doc/config/http.html

//编辑:

您还可以尝试在web.xml(或java类)中设置编码过滤器,如:
Spring/Rest @PathVariable character encoding

原文地址:https://www.jb51.cc/spring/432342.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐