I am new with Spring framework and right now I'm trying to learn few things but facing a problem. (Learning from this tutorial https://spring.io/guides/gs/rest-service/ ). So by default, I can launch my program via localhost../greeting
, but what if I want to change the name not like in the website with /greeting?name=xx
, but for example /greeting/Tom..
( /greeting/{name}
)
The thing you need here is called PathVariable. This article is very useful to understand all the difference between types of requesting
https://www.quora.com/What-is-the-difference-between-QueryParam-and-pathParam-in-Webservices
In GreetingController:
Instead of this:
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name)
Implement this:
@RequestMapping("/greeting/{name}")
public Greeting greeting(@PathVariable("name") String name)
for optional path variable:
@PathVariable Optional<String > name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.