【问题标题】:How to handle requests that contain forward slashes?如何处理包含正斜杠的请求?
【发布时间】:2017-10-16 08:55:57
【问题描述】:

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

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

以下代码不起作用:

@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 无法处理此请求。

【问题讨论】:

  • 你能写成:(localhost:8080/login/verify/212/32/cntv5tag07rmy791wbme7xa8x,/…) 吗?其中“212”、“32”和“cntv5tag07rmy791wbme7xa8x,/SSNZclzqhhH7v6uHIkUsIcPusKo=”将是不同的参数。你愿意吗?
  • 感谢您的回答。不,我需要解析整个字符串,其中包含 4 个参数,划分为“,”。有时 4 参数可以包含斜杠(/)

标签: java spring model-view-controller


【解决方案1】:

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

@RequestMapping(value = "/login/verify/**",

然后,在body中获取值,你将使用

String str = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)

示例代码:

@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)
}

【讨论】:

  • 没错!谢谢。其中请求是 HttpServletRequest。 @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)}
【解决方案2】:

试试这个网址:http://localhost:8080/login/verify?req=212,32,/cntv5tag07rmy791wbme7xa8x,/SSNZclzqhhH7v6uHIkUsIcPusKo=

然后这样处理:

@RequestMapping("/login/verify")
public String test(@RequestParam("req") String data) {
    //'data' will contains '212,32,/cntv5tag07rmy791wbme7xa8x,/SSNZclzqhhH7v6uHIkUsIcPusKo='
   String params[] = data.split(",");
}

【讨论】:

  • 我不能,请求不是由我形成的。我不能使用查询参数
【解决方案3】:

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

@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);

}

【讨论】:

  • 我知道这个解决方案,但在这种情况下,我得到 string1 = 212, string2 =32, string3 = cntv5tag07rmy791wbme7xa8x, string4 = SSNZclzqhhH7v6uHIkUsIcPusKo=。没有斜杠(/)的字符串 3 和字符串 4,在这种情况下我需要代码中的额外条件来处理这个问题
  • 你可以使用这样的东西,@RequestMapping("/login/verify/**"),使用request.getRequestURI()然后解析它。
猜你喜欢
  • 2015-10-03
  • 2012-12-19
  • 1970-01-01
  • 2012-06-28
  • 2015-07-01
  • 1970-01-01
  • 2018-01-10
  • 2018-02-10
  • 2012-01-17
相关资源
最近更新 更多