-2

I have this class

@RestController
public class TestController {

    @RequestMapping(value = "/info/{login}", method = RequestMethod.GET)
    public @ResponseBody String getLogin() {
       String login = ?
       return login;
    }
}

Can I parse somehow value of {login}?

For example:

http://localhost:8080/info/1234

my method return "1234";

http://localhost:8080/info/298

my method return "298".

Jens
  • 67,715
  • 15
  • 98
  • 113

2 Answers2

1

You just need to declare a variable for the login value and annotate that with @PathVariable:

@RestController
public class TestController {

  @RequestMapping(value = "/info/{login}", method = RequestMethod.GET)
  public @ResponseBody String getLogin(@PathVariable("login") String login) {
    return login;
  }
}

Baeldung has good explanations about the powerful options: https://www.baeldung.com/spring-requestmapping#path-variable

Thomas Preißler
  • 613
  • 4
  • 13
1

Yes, you can get that value. Replace your code by below.

  @RequestMapping(value = "/info/{login}", method = RequestMethod.GET)
public @ResponseBody String getLogin(@PathVariable String login) {
   return login;
}
P.Sanjay
  • 303
  • 1
  • 7