我有一个基本的 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
最佳答案
您不能将值注入(inject)静态变量。
尝试更改为:
public static String WEATHER_URL;
@Value(“${weather.url}")
public void setWehtherUrl(String url) {
WEATHER_URL= url;
}
关于java - 初始化服务时URI不是绝对的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52355720/