0

How can the callbackFacebook function get the value of code from the 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";
}
Alien
  • 15,141
  • 6
  • 37
  • 57
Thai Van
  • 1
  • 2
  • 1
    Possible duplicate of [How do I retrieve query parameters in Spring Boot?](https://stackoverflow.com/questions/32201441/how-do-i-retrieve-query-parameters-in-spring-boot) – gp. Dec 18 '18 at 00:23
  • After `?`, its query, not path. Yon can find out syntax of URI from here. : https://en.wikipedia.org/wiki/URL. . – Min Hyoung Hong Dec 18 '18 at 04:25

3 Answers3

1

Try this. code is a query parameter judging by your URL, not a path variable. Path variables are a part of the path itself (i.e. if your URL was something like /{code}/callback, then code is a PathVariable).

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

If your URL is http://localhost:8081/callback?code=AQDNm6hezKdTsId5k4oXKNo then it is case of request parameters so the method will be like below.

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

If your URL is http://localhost:8081/callback/AQDNm6hezKdTsId5k4oXKNo then then it is case of path variables method will be like below.

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

Refer requestparam-vs-pathvariable for better clarity.

Alien
  • 15,141
  • 6
  • 37
  • 57
0

I will explain 2 ways.

1-If it is added in the session in somewhere in the project as attribute,You can get it like this :

  @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";
    }

example output : AQDNm6hezKdTsId5k4oXKNo

2-You can directly get URL.But then you need to parse URL.Because all URL is coming.

@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";
    }

example output : http://localhost:8081/callback?code=AQDNm6hezKdTsId5k4oXKNo

Cocuthemyth
  • 264
  • 1
  • 2
  • 11