初始化服务时URI不是绝对的

时间:2018-09-16 15:39:16

标签: java spring spring-mvc spring-boot

我有一个基本的SpringBoot 2.0.5.RELEASE应用程序。使用Spring Initializer,JPA,嵌入式Tomcat,Thymeleaf模板引擎并将其打包为可执行JAR文件。

我已经创建了此服务:

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



    }

..
}

但是当我启动应用程序时。我收到此错误:

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

1 个答案:

答案 0 :(得分:2)

您不能将值注入静态变量。

尝试更改为此:

.id