URI is not absolute when init a service

4203 views java
1

I have a basic SpringBoot 2.0.5.RELEASE app. Using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file.

I have created this service:

@Service
public class WeatherService  {



    @Value(“${weather.url}")
    public static String WEATHER_URL;


    static Double temperature; 


    static {


        temperature = new RestTemplate().getForEntity(WEATHER_URL, Double.class).getBody();



    }

..
}

but when I start the app. I got this error:

Caused by: java.lang.IllegalArgumentException: URI is not absolute
    at java.net.URI.toURL(URI.java:1088)
    at org.springframework.http.client.SimpleClientHttpRequestFactory.createRequest(SimpleClientHttpRequestFactory.java:145)
    at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:87)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:683)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:644)
    at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:323)
    at com.tdk.backend.service.WeatherService.<clinit>(WeatherService.java:54)
    ... 84 common frames omitted

answered question

1 Answer

11

You can not inject value into static variable.

Try change to this:

public static String WEATHER_URL;

@Value(“${weather.url}")
public void setWehtherUrl(String url) {
    WEATHER_URL= url;
}

posted this

Have an answer?

JD

Please login first before posting an answer.