春季:我如何从uri中获得价值?

时间:2018-12-18 00:12:22

标签: spring-boot

callbackFacebook 函数如何从uri获取代码的价值?

uri = http://localhost:8081/callback?code=AQDNm6hezKdTsId5k4oXKNo

@RequestMapping(value = "/callback?{code}", method = RequestMethod.GET)

public String callbackFacebook(Model model, @PathVariable(name = "code") String code) {

    System.out.println(code);
    return "login";
}

3 个答案:

答案 0 :(得分:1)

尝试一下。 code是根据您的URL而非路径变量判断的查询参数。路径变量是路径本身的一部分(即,如果您的URL类似于/{code}/callback,则代码是PathVariable)。

@RequestMapping(value = "/callback", method = RequestMethod.GET)
public String callbackFacebook(Model model, @RequestParam(value = "code") String code) {
        System.out.println(code);
        return "login";
}

答案 1 :(得分:0)

如果您的URL为http://localhost:8081/callback?code=AQDNm6hezKdTsId5k4oXKNo,则为请求参数,因此方法如下。

@RequestMapping(value = "/callback", method = RequestMethod.GET)
public String callbackFacebook(Model model, @RequestParam(value = "code") String code) {
        return "login";
}

如果您的URL是http://localhost:8081/callback/AQDNm6hezKdTsId5k4oXKNo,则路径变量的情况将如下所示。

@RequestMapping(value = "/callback/{code}", method = RequestMethod.GET)
public String callbackFacebook(Model model, @PathVariable(value = "code") String code) {
        return "login";
}

请参阅requestparam-vs-pathvariable,以获得更好的清晰度。

答案 2 :(得分:0)

我将解释两种方法。

1-如果将它添加到会话中项目的某个地方作为属性,则可以这样获得:

  @RequestMapping(value = "/callback?{code}", method = RequestMethod.GET)
    public String callbackFacebook(Model model, @PathVariable(name = "code") String code,HttpServletRequest request) {

        String code1 = request.getSession().getAttribute("code").toString();


        return "login";
    }

示例输出:AQDNm6hezKdTsId5k4oXKNo

2-您可以直接获取URL。但是随后您需要解析URL。因为所有URL都将出现。

@RequestMapping(value = "/callback?{code}", method = RequestMethod.GET)
    public String callbackFacebook(Model model, @PathVariable(name = "code") String code,HttpServletRequest request) {

        StringBuffer requestURL = request.getRequestURL();

        return "login";
    }

示例输出:http://localhost:8081/callback?code=AQDNm6hezKdTsId5k4oXKNo