gpt4 book ai didi

java - 无法解析字符串值中自定义对象的占位符列表

转载 作者:行者123 更新时间:2023-12-05 07:44:04 26 4
gpt4 key购买 nike

我无法在 spring rest Controller 中读取如下所示的自定义对象列表。但是,能够通过将 bean Autowiring 到 restcontoller 中的 java bean 来读取/获取数据。任何人都可以帮助我使用 annonation @value 从 rest controller 读取数据列表吗?

无法解析字符串值“${royalty.testRates}”中自定义对象 (royalty.testRates) 的占位符列表。

YAML 文件的代码片段:

testRates:
- channelType: "WEB"
value: "0.03"
- channelType: "ANDROID_TAB"
value: "0.04"
- channelType: "ANDROID_PHONE"
value: "0.04"

java 文件的代码片段:Java bean :

@EnableAsync
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(ignoreInvalidFields = false, prefix = "royalty")
@Component
public class RoyaltyMigration {

private List<TestRate> testRates = new ArrayList<TestRate>();

public static class TestRate {
private String channelType;
private String value;

public List<TestRate> getTestRates() {
return testRates;
}

public void setTestRates(List<TestRate> testRates) {
this.testRates = testRates;
}

public String getChannelType() {
return channelType;
}

public void setChannelType(String channelType) {
this.channelType = channelType;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}

Spring 休息 Controller :

//@Autowired//Commented
//RoyaltyMigration royaltyMigration; //Commented

@Value("${royalty.testRates}")
private List<RoyaltyMigration.TestRate> testRates;//= new ArrayList<RoyaltyMigration.TestRate>();;

public void setTestRates(List<RoyaltyMigration.TestRate> testRates) {
this.testRates = testRates;
}
@RequestMapping(value = "/testrates", method = {RequestMethod.GET, RequestMethod.POST},
produces = "application/json")
public ResponseEntity<RoyaltyMigration> testRates() {
final RoyaltyMigration royaltyMigration = new RoyaltyMigration();
royaltyMigration.setTestRates(this.testRates);
return new ResponseEntity<>(royaltyMigration, HttpStatus.OK);
}

在此处记录跟踪 –

2017-04-02 21:38:46,366 [main] org.springframework.boot.SpringApplication ERROR Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'royaltyMigrationController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List com.royalty.controller.RoyaltyMigrationController.testRates; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'royalty.testRates' in string value "${royalty.testRates}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:760)
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:360)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:306)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)
at com.expedia.www.host.loyalty.Starter.main(Starter.java:23)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List com.royalty.controller.LoyaltyMigrationController.testRates; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'loyalty.testRates' in string value "${royalty.testRates}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 17 more
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'royalty.testRates' in string value "${royalty.testRates}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:808)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1027)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
... 19 more

最佳答案

把你的 yaml 改成这样

royalty:
testRates:
- channelType: "WEB"
value: "0.03"
- channelType: "ANDROID_TAB"
value: "0.04"
- channelType: "ANDROID_PHONE"
value: "0.04"

然后在你的 Controller 中删除这些行

@Value("${royalty.testRates}")
private List<RoyaltyMigration.TestRate> testRates;//= new ArrayList<RoyaltyMigration.TestRate>();;

public void setTestRates(List<RoyaltyMigration.TestRate> testRates) {
this.testRates = testRates;
}

并添加这些

 @Autowired
RoyaltyMigration royaltyMigration;

到你的 Controller 。并使用 royaltyMigration.getTestRates() 获取测试率。

关于java - 无法解析字符串值中自定义对象的占位符列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43176061/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com