Java 带有{}大括号的Spring MVC @Path变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26794198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:17:38  来源:igfitidea点击:

Spring MVC @Path variable with { } braces

javaspringspring-mvcspring-boot

提问by Aravind Cheekkallur

Am developing an application using spring boot. In REST controller i prefer to use path variable(@PathVariabaleannotation). My code fetching the path variable but it contatins { }braces as it is in the url. Please any one suggest me to solve this issue

正在使用 spring boot 开发应用程序。在 REST 控制器中,我更喜欢使用路径变量(@PathVariabale注释)。我的代码获取路径变量,但它包含{ }大括号,因为它在 url 中。请任何人建议我解决这个问题

@RequestMapping(value = "/user/item/{loginName}", method = RequestMethod.GET)
public void getSourceDetails(@PathVariable String loginName) {
    try {
        System.out.println(loginName);
        // it print like this  {john}
    } catch (Exception e) {
        LOG.error(e);
    }
}

URL

网址

http://localhost:8080/user/item/{john}

Out put in controller

出入控制器

{john}

{约翰}

采纳答案by Jaskey

Use http://localhost:8080/user/item/johnto submit your request instead.

使用http://localhost:8080/user/item/john提交请求来代替。

You give Spring a value of "{john}" to the path variable loginName, so Spring get it with the "{}"

您为路径变量赋予 Spring 值“{john}” loginName,因此 Spring 使用“{}”获取它

Web MVC frameworkstates that

Web MVC 框架指出

URI Template Patterns

URI templates can be used for convenient access to selected parts of a URL in a @RequestMapping method.

A URI Template is a URI-like string, containing one or more variable names. When you substitute values for these variables, the template becomes a URI. The proposed RFC for URI Templates defines how a URI is parameterized. For example, the URI Template http://www.example.com/users/{userId} contains the variable userId. Assigning the value fred to the variable yields http://www.example.com/users/fred.

In Spring MVC you can use the @PathVariable annotation on a method argument to bind it to the value of a URI template variable:

URI 模板模式

URI 模板可用于在 @RequestMapping 方法中方便地访问 URL 的选定部分。

URI 模板是一个类似于 URI 的字符串,包含一个或多个变量名称。当您替换这些变量的值时,模板将成为 URI。为 URI 模板提议的 RFC 定义了如何参数化 URI。例如,URI 模板 http://www.example.com/users/{userId}包含变量 userId将值 fred 分配给变量会产生 http://www.example.com/users/fred

在 Spring MVC 中,您可以在方法参数上使用 @PathVariable 注释将其绑定到 URI 模板变量的值:

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
 public String findOwner(@PathVariable String ownerId, Model model) {
     Owner owner = ownerService.findOwner(ownerId);
     model.addAttribute("owner", owner);
     return "displayOwner"; 
  }

The URI Template " /owners/{ownerId}" specifies the variable name ownerId. When the controller handles this request, the value of ownerId is set to the value found in the appropriate part of the URI. For example, when a request comes in for /owners/fred, the value of ownerId is fred.

URI 模板“/owners/{ownerId}”指定变量名称 ownerId。当控制器处理这个请求时,ownerId 的值被设置为在 URI 的适当部分找到的值。例如,当请求 /owners/fred 时,ownerId 的值为 fred。