Java 带{}大括号的Spring MVC@Path变量
我正在使用spring boot开发一个应用程序。在REST控制器中,我更喜欢使用路径变量(Java 带{}大括号的Spring MVC@Path变量,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,我正在使用spring boot开发一个应用程序。在REST控制器中,我更喜欢使用路径变量(@pathVariablea注释)。我的代码正在获取path变量,但它包含{}大括号,因为它在url中。请任何人建议我解决这个问题 @RequestMapping(value = "/user/item/{loginName}", method = RequestMethod.GET) public void getSourceDetails(@PathVariable String loginName)
@pathVariablea
注释)。我的代码正在获取path变量,但它包含{}大括号,因为它在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);
}
}
网址
输出输入控制器
{john}使用
http://localhost:8080/user/item/john
以提交您的请求
您为路径变量loginName
赋予Spring一个值“{john}”,因此Spring使用“{}”来获取它
声明
URI模板模式
URI模板可用于方便地访问选定的部分
@RequestMapping方法中的URL
URI模板是一个类似于URI的字符串,包含一个或多个变量
名字替换这些变量的值时,模板
成为URI。建议的RFC for URI模板定义了URI的使用方式
参数化。例如,URI模板
{userId}包含变量userId。
将值fred分配给变量收益率
.
在SpringMVC中,可以在方法上使用@PathVariable注释
参数将其绑定到URI模板变量的值:
URI模板“/owners/{ownerId}”指定变量名
所有者身份。当控制器处理此请求时
ownerId设置为URI的适当部分中的值。
例如,当/owner/fred的请求传入时
所有者是弗雷德
你想打印什么?@SotiriosDelimanolis我需要john而不是{john}
http://localhost:8080/user/item/{john}
@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";
}