UriComponentsBuilder不会扩展@Value属性

时间:2018-10-25 16:44:24

标签: spring spring-mvc spring-boot spring-scheduled

我正在尝试像这样从属性文件application.yml组成一个网址:

service:
  url: http://localhost:8081/rest/change/age

我有一个如下的调度程序:

@Component
public class AScheduler {

private static final Logger logger = LoggerFactory.getLogger(AScheduler.class);

private String serviceUrl;

private final RestTemplate restTemplate;

HttpHeaders headers = new HttpHeaders();
HttpEntity<?> httpEntity = new HttpEntity<>(headers);

public AScheduler(RestTemplateBuilder restTemplateBuilder,
                     @Value("{service.url}") String serviceUrl) {
    this.restTemplate = restTemplateBuilder.build();
    this.serviceUrl = serviceUrl;
}

 @Scheduled(fixedRate = 30 * 1000)
public void invoke() {
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(serviceUrl);
    ResponseEntity<JsonNode> jsonResponse = (ResponseEntity<JsonNode>) getRestResponseFromUriString(builder.toUriString(),
            JsonNode.class);
}

我不断收到以下异常:

  

java.lang.IllegalArgumentException:[{service.url}]不是有效的HTTP URL       在org.springframework.web.util.UriComponentsBuilder.fromHttpUrl(UriComponentsBuilder.java:290)〜[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]       在com.salesforce.fire.adcl.scheduler.AScheduler.invoke(AScheduler.java:47)〜[classes /:na]       在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)〜[na:1.8.0_152]       在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_152]       在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_152]       在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_152]       在org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)〜[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]       在org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)〜[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]       在java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511)[na:1.8.0_152]       在java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)[na:1.8.0_152]       在java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.access $ 301(ScheduledThreadPoolExecutor.java:180)[na:1.8.0_152]       在java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)[na:1.8.0_152]       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)[na:1.8.0_152]       在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)[na:1.8.0_152]       在java.lang.Thread.run(Thread.java:748)[na:1.8.0_152]

我尝试了buildAndExpand()和许多其他方法来构成UriComponent。我虽然无法在构建器中使用此属性。有没有一种方法可以使用属性文件中的UrlComponentsBuilder组成一个网址

1 个答案:

答案 0 :(得分:2)

您在@Value的SpEL中犯了一个错误。您必须以以下格式编写属性名称:${property.name}

所以改变

@Value("{service.url}") String serviceUrl 

@Value("${service.url}") String serviceUrl