首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何处理包含正斜杠的请求?

如何处理包含正斜杠的请求?
EN

Stack Overflow用户
提问于 2017-05-17 04:31:43
回答 3查看 1.7K关注 0票数 0

我的URL请求是http://localhost:8080/login/verify/212,32,/cntv5tag07rmy791wbme7xa8x,/SSNZclzqhhH7v6uHIkUsIcPusKo=

我需要得到以下部分:**212,32,/cntv5tag07rmy791wbme7xa8x,/SSNZclzqhhH7v6uHIkUsIcPusKo=**

以下代码不起作用:

代码语言:javascript
复制
@RequestMapping(value = "/login/verify/{request:.+}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public ResponseEntity verifyLogin(@PathVariable(value = "request") String request)
            throws InvalidSignatureException
    {
}

错误: HTTP状态404。

Spring无法处理此请求。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-05-17 06:43:20

若要将uri与斜杠匹配,请使用double *

代码语言:javascript
复制
@RequestMapping(value = "/login/verify/**",

然后,在正文中获取值,您将使用

代码语言:javascript
复制
String str = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)

样本代码:

代码语言:javascript
复制
@RequestMapping(value = "/login/verify/**", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) 
public ResponseEntity verifyLogin(HttpServletRequest httpServletRequest) throws InvalidSignatureException {
    String str = (String) request.getAttribute( HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)
}
票数 1
EN

Stack Overflow用户

发布于 2017-05-17 05:20:29

尝试以下网址:http://localhost:8080/login/verify?req=212,32,/cntv5tag07rmy791wbme7xa8x,/SSNZclzqhhH7v6uHIkUsIcPusKo=

像这样处理它:

代码语言:javascript
复制
@RequestMapping("/login/verify")
public String test(@RequestParam("req") String data) {
    //'data' will contains '212,32,/cntv5tag07rmy791wbme7xa8x,/SSNZclzqhhH7v6uHIkUsIcPusKo='
   String params[] = data.split(",");
}
票数 0
EN

Stack Overflow用户

发布于 2017-05-17 06:26:01

您的url中有正斜杠,这些字符串将被视为路径变量。如果有可能只有3个路径变量,请尝试下面的代码。请看一下herehere

代码语言:javascript
复制
@RequestMapping(value = {"/login/verify/{string1:.+}",
        "/login/verify/{string1:.+}/{string2:.+}",
        "/login/verify/{string1:.+}/{string2:.+}/{string3:.+}"}, method = RequestMethod.POST)
  public ResponseEntity verifyLogin(HttpServletRequest request, HttpServletResponse httpresponse, 
        @PathVariable("string1") String string1,
        @PathVariable("string2") String string2,
        @PathVariable("string3") String string3) {
    System.out.println("***************************************************I am called: "+string1+" "+string2+" "+string3);

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44015637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档