[英]Cannot get '#' symbol in Controller using Spring @RequestParam
[英]Cannot get value in Controller using Spring
我有一个 Controller 如下所示,在添加@Value("${code.url}") final String url
,我遇到以下错误:
无法解析 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean 的值“${code.url}”中的占位符“code.url”
该问题显然与阅读application.yml
中的code.url
中的问题有关。
应用程序.yml:
code:
url: "https://www.gmail.com/"
controller:
@RestController
@RequestMapping("/api/v1")
public class DemoController {
private final DemoService demoService;
private final Clock clock;
private final String url;
public DemoController(final DemoService demoService, final Clock clock,
@Value("${code.url}") final String url) {
this.demoService = demoService;
this.clock = clock;
this.url = url;
}
//code omitted
}
我还尝试在 controller 中使用@RequiredArgsConstructor
,但没有任何意义。 那么,我该如何解决这个问题呢?
将您的代码更改为:
@RestController
@RequestMapping("/api/v1")
public class DemoController {
private final DemoService demoService;
private final Clock clock;
@Value("${code.url}")
private final String url;
public DemoController(final DemoService demoService,
final Clock clock) {
this.demoService = demoService;
this.clock = clock;
}
//code omitted
}
从改变格式
code:
url: "https://www.gmail.com/"
至
code:
url: "https://www.gmail.com/"
您需要正确缩进 yaml:
code:
url: "https://www.gmail.com/"
否则url
处于最高级别,属性将为url
而不是code.url
在 YAML 文件中使用缩进..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.