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

时间:2017-05-17 04:31:43

标签: java spring model-view-controller

我的网址请求为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无法处理此请求。

3 个答案:

答案 0 :(得分:1)

您的网址中包含正斜杠,这些字符串将被视为路径变量。如果您可能只有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);

}

答案 1 :(得分:1)

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

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

然后,在身体中获取值,您将使用

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

答案 2 :(得分:0)

请尝试使用此网址: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(",");
}