0
votes

I am trying to compose a Url from a property file application.yml like this:

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

I have a scheduler like the following:

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

I keep getting the following exception:

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

I have tried buildAndExpand()and many other ways of composing UriComponents. I am though unable to consume this property in the builder. Is there a way I can compose a Url using UrlComponentsBuilder from a property file

1
Voting to close for typo. The correct value is: @Value("${service.url}"). You forgot the $JB Nizet

1 Answers

3
votes

You've made a mistake in SpEL of the @Value. You have to write the property name in the following format: ${property.name}

So change

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

to

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