简体   繁体   English

如何从 Spring 应用程序中的 URL 获取延续映射?

[英]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.

相关问题 Spring MVC-如何使用不同的映射注释调用相同的URL - Spring MVC - How do I call the same URL with different mapping annotations 在Spring MVC中的servlet映射中,如何映射url模式目录的根? - In a servlet mapping in Spring MVC how do I map the root of a url pattern directory? 如何从 Elastic Beanstalk 的 Spring Boot 应用程序中获取客户端主机名和/或客户端 IP 地址? - How do I get the Client Hostname and/or client IP address from within a Spring Boot application in Elastic Beanstalk? Spring Boot - 如何获取请求的基本URL - Spring Boot - how do I get the base url of a request 在Spring Web中,如何获取@RestController的完整URL? - In Spring Web, how do I get the full URL of a @RestController? 如何在Spring Boot中执行多个URL映射(别名) - How to do Multiple URL Mapping (aliases) in Spring Boot 默认URL映射Secure Spring MVC应用程序 - Default URL Mapping Secure Spring MVC application 我如何有效地禁止用户使用 Spring Web 应用程序 - How do i ban a user from a spring web application effectively 在 RESTful Spring 引导应用程序中,如何对嵌套的 object 属性进行 url 查询? - In a RESTful Spring Boot application, how do I do url queries of nested object properties? Spring 3.x - 如何从返回数据的映射重定向? - Spring 3.x - how do I redirect from a mapping that returns data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM