cuốn sách gpt4 ai đã làm

java - 在哪里以及如何解码@PathVariable

In lại Tác giả: Taklimakan 更新时间:2023-11-03 04:34:14 26 4
mua khóa gpt4 Nike

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

/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) {
thử {
return DAO.getItemByIdentifier(URLDecoder.decode(identifier, SOME_ENCODING))
}
catch (UnsupportedEncodingException e) {
...
}

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

@RequestMapping(value = "/path/items/{identifier}", method = RequestMethod.GET)
public Item get(@PathVariable String identifier) {
return DAO.getitemByidentifier(identifier); // already decoded!
}

câu trả lời hay nhất

bạn có thể web.xml 中使用 Spring 的 CharacterEncodingFilter 类作为过滤器,如下所示:


CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8


forceEncoding
true


<ánh xạ="" bộ="" lọc="">
CharacterEncodingFilter
/*

关于java - 在哪里以及如何解码@PathVariable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25944964/

26 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress