【发布时间】:2019-06-19 03:41:12
【问题描述】:
我在 .jar 文件中运行我的应用程序时遇到问题。当我在 IntelliJ 中运行它时,我没有任何问题。这是通过 .jar 运行我的应用程序的结果我该如何解决?我正在使用 JsonNode 和 restTemplate 从 json 获取信息并创建对象。
Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
at com.rafalpodgorski.weatherclient.WeatherClientApplication.main(WeatherClientApplication.java:17) ~[classes!/:0.0.1-SNAPSHOT]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) ~[weatherclient-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) ~[weatherclient-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) ~[weatherclient-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) ~[weatherclient-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
Caused by: org.springframework.web.client.HttpClientErrorException$NotFound: 404 Not Found
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:85) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:777) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:310) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
at com.rafalpodgorski.weatherclient.service.TemperatureService.getTemperature(TemperatureService.java:42) ~[classes!/:0.0.1-SNAPSHOT]
at com.rafalpodgorski.weatherclient.service.TemperatureService.getTemperatureByCityName(TemperatureService.java:31) ~[classes!/:0.0.1-SNAPSHOT]
at com.rafalpodgorski.weatherclient.WeatherClientApplication.getTemperatureForCity(WeatherClientApplication.java:24) ~[classes!/:0.0.1-SNAPSHOT]
at com.rafalpodgorski.weatherclient.WeatherClientApplication.lambda$run$0(WeatherClientApplication.java:37) ~[classes!/:0.0.1-SNAPSHOT]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
... 13 common frames omitted
我的跑步课:
@SpringBootApplication(scanBasePackages = "com.application")
public class WeatherClientApplication {
public static void main(String[] args) {
SpringApplication.run(WeatherClientApplication.class, args);
}
private static Temperature getTemperatureForCity(TemperatureService temperatureService, String city) {
if (city.equals("auto")) {
return temperatureService.getTemperatureByGeolocation();
} else {
return temperatureService.getTemperatureByCityName(city);
}
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public CommandLineRunner run(TemperatureService temperatureService) {
return args -> {
for (String city : ConfigReader.readConfig()) {
Temperature temperature = getTemperatureForCity(temperatureService, city);
System.out.println(temperature);
temperatureService.saveTemperature(temperature);
}
};
}
}
温度服务:
@Service
public class TemperatureService {
private RestTemplate restTemplate;
private GeolocationService geolocationService;
public TemperatureService(RestTemplate restTemplate, GeolocationService geolocationService) {
this.restTemplate = restTemplate;
this.geolocationService = geolocationService;
}
public Temperature getTemperatureByCityName(String cityName) {
String url = String.format("http://api.openweathermap.org/data/2.5/weather?q=%s&APPID=%s&units=metric", cityName, API_KEY);
return getTemperature(url);
}
public Temperature getTemperatureByGeolocation() {
Location location = geolocationService.getCoords();
double lat = location.getLat();
double lng = location.getLng();
String url = String.format("http://api.openweathermap.org/data/2.5/weather?lat=%.2f&lon=%.2f&APPID=%s&units=metric", lat, lng, API_KEY);
return getTemperature(url);
}
private Temperature getTemperature(String url) {
JsonNode info = restTemplate.getForObject(url, JsonNode.class);
String cityName = info.get("name").asText();
int temperature = info.get("main").get("temp").asInt();
return new Temperature(cityName, temperature);
}
}
我认为 JsonNode 有问题,因为当我使用 map 进行映射时,不会出现此问题。
【问题讨论】:
标签: java spring spring-boot jar