2017-04-03 57 views
2

我无法在弹簧休息控制器中读取下面的自定义对象列表。但是,能够通过将bean自动装入restcontoller中的java bean来读取/获取数据。 任何人都可以帮助我使用annonation @value从rest控制器读取数据列表吗?无法解析占位符字符串值中的自定义对象列表

无法解析占位符字符串值“$ {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; 
    } 

} 

弹簧安置控制器:

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

回答

0

改变你的YAML喜欢这个

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

而在你的控制器中删除这些行

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

到控制器。并使用royaltyMigration.getTestRates()来获取testRates。

相关问题