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

huangapple go评论76阅读模式
英文:

How do I get the continuation mapping from the URL in a Spring application?

问题

我有这个Spring应用程序,我想从URL中获取值,例如:

发送到Spring应用程序的URL是www.example.com/app/account/register。当我的代码看起来像这样时,我如何获取/account之后的/register部分。

对于这个示例,我使用了/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;
    }
}
英文:

I have this spring application where I want to get the values from a URL, for example:

The url send to the spring application is www.example.com/app/account/register. How do I get the /register part after /account when my code looks like this.

For this example I used /register but this can be /login, /something as well.

@RestController
@RequestMapping(&quot;/app&quot;)
public class MainController {

    @RequestMapping(value = &quot;/account&quot;)
    public boolean AccountServer(@RequestHeader HttpHeaders httpHeaders, @RequestBody Map&lt;String, String&gt; payLoad){
        return true;
    }
}

答案1

得分: 3

你可以使用 @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;
    }
}
英文:

You can use the @PathVariable annotation to get the value.

@RestController
@RequestMapping(&quot;/app&quot;)
public class MainController {

    @RequestMapping(value = &quot;/account/{operation}&quot;)
    public boolean AccountServer(@RequestHeader HttpHeaders httpHeaders,
                                 @RequestBody Map&lt;String, String&gt; payLoad,
                                 @PathVariable(&quot;operation&quot;) String operation){
  
        return true;
    }
}

答案2

得分: 1

Use UriComponentsBuilder作为参数它将由Spring注入并初始化为当前URI然后您可以将其转换为UriComponents [UriComponents][1]以查询路径

    @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;
        }
    }

[1]: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/util/UriComponents.html
英文:

Use UriComponentsBuilder as a parameter, it will be injected by Spring and initialized with current URI. You can then convert to UriComponents UriComponents to query the path.

@RestController
@RequestMapping(&quot;/app&quot;)
public class MainController {

    @RequestMapping(value = &quot;/account&quot;)
    public boolean AccountServer(UriComponentsBuilder builder, @RequestHeader HttpHeaders httpHeaders, @RequestBody Map&lt;String, String&gt; payLoad){
        List&lt;String&gt; pathSegments = builder.build().getPathSegments();
        ...
        return true;
    }
}

huangapple
  • 本文由 发表于 2020年9月5日 20:27:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63753927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定