[英]How do I get the continuation mapping from the URL in a Spring application?
I have this spring application where I want to get the values from a URL, for example:我有这个 spring 应用程序,我想从 URL 获取值,例如:
The url send to the spring application is www.example.com/app/account/register
.发送到 spring 应用程序的 url 是
www.example.com/app/account/register
。 How do I get the /register
part after /account
when my code looks like this.当我的代码看起来像这样时,如何在
/account
之后获取/register
部分。
For this example I used /register
but this can be /login
, /something
as well.在这个例子中,我使用了
/register
但这也可以是/login
, /something
。
@RestController
@RequestMapping("/app")
public class MainController {
@RequestMapping(value = "/account")
public boolean AccountServer(@RequestHeader HttpHeaders httpHeaders, @RequestBody Map<String, String> payLoad){
return true;
}
}
You can use the @PathVariable
annotation to get the value.您可以使用
@PathVariable
注释来获取值。
@RestController
@RequestMapping("/app")
public class MainController {
@RequestMapping(value = "/account/{operation}")
public boolean AccountServer(@RequestHeader HttpHeaders httpHeaders,
@RequestBody Map<String, String> payLoad,
@PathVariable("operation") String operation){
return true;
}
}
Use UriComponentsBuilder as a parameter, it will be injected by Spring and initialized with current URI.使用 UriComponentsBuilder 作为参数,它将被 Spring 注入并使用当前 URI 进行初始化。 You can then convert to UriComponents UriComponents to query the path.
然后可以转换为 UriComponents UriComponents来查询路径。
@RestController
@RequestMapping("/app")
public class MainController {
@RequestMapping(value = "/account")
public boolean AccountServer(UriComponentsBuilder builder, @RequestHeader HttpHeaders httpHeaders, @RequestBody Map<String, String> payLoad){
List<String> pathSegments = builder.build().getPathSegments();
...
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.