加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:31:42 所属栏目:大数据 来源:网络整理
导读:客户端在服务器上发送(实现无关紧要): /path/items/ + urlencode(id,SOME_ENCODING) 考虑结果URL将是: /path/items/my%2Fkey 因此我在服务器上: @RequestMapping(value = "/path/items/{identifier}",method = RequestMethod.GET)public Item get(@PathVa

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

/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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读