简体   繁体   English

无法通过链接春季到达

[英]Can't reach by link Spring

I am new with Spring framework and right now I'm trying to learn few things but facing a problem. 我是Spring框架的新手,现在我想学习一些东西,但面临一个问题。 (Learning from this tutorial https://spring.io/guides/gs/rest-service/ ). (从本教程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} ) 因此,默认情况下,我可以通过localhost../greeting启动程序,但是如果我不想使用/greeting?name=xx来更改网站名称而不是网站中的/greeting?name=xx ,例如/greeting/Tom../greeting/{name}

The thing you need here is called PathVariable. 您在这里需要的东西称为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 https://www.quora.com/What-is-the-difference-between-QueryParam-and-pathParam-in-Webservices

In GreetingController: 在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM