带有{}大括号的Spring MVC @Path变量 [英] Spring MVC @Path variable with { } braces

查看:1338
本文介绍了带有{}大括号的Spring MVC @Path变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Am developing an application using spring boot. In REST controller i prefer to use path variable(@PathVariabale annotation). 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

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

输入控制器

{john}

推荐答案

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

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

你给Spring一个路径变量 loginName 的值为{john},所以Spring得到它{}

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

Web MVC框架声明


URI模板模式



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

URI Template Patterns

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

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

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.

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

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



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




URI模板/ owners / {ownerId }指定变量名称
ownerId。当控制器处理此请求时,
ownerId的值将设置为URI的相应部分中找到的值。
例如,当/ owner / fred的请求进入时,
ownerId的值为fred。

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.

这篇关于带有{}大括号的Spring MVC @Path变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆