我使用 Java 8 并具有以下代码:
public WeatherDTO(Map<?,?> mappedJsonData) {
if (mappedJsonData == null || mappedJsonData.isEmpty()) {
return;
}
List<?> weather = (List<?>)mappedJsonData.get("weather");
if (weather != null && !weather.isEmpty()) {
this.weather = (String) ((Map<?,?>)weather.get(0)).get("description");
}
Map<?,?> jsonMain = (Map<?,?>)mappedJsonData.get("main");
if (jsonMain != null && !jsonMain.isEmpty()) {
this.temperature = (double)jsonMain.get("temp") - 273.0;
this.humidity = (int)jsonMain.get("humidity");
}
}
当我在 Eclipse 和嵌入式 Tomcat 中运行 Web 应用程序时,此代码工作正常。 Eclipse Problems View 中也没有显示任何问题。
但是当我从我的 shell/bash 启动 maven 编译时,我收到以下错误:
[错误] 编译错误: [信息]-------------------------------------------- --------------
[错误]/home/xxx/xxx/xxx/src/main/java/xxx/xxx/xxx/weather/WeatherDTO.java:[52,64] 不兼容类型:捕获#1 of ?不能转换成double
[错误]/home/xxx/xxx/xxx/src/main/java/xxx/xxx/xxx/weather/WeatherDTO.java:[53,58] 不兼容的类型:capture#2 of ?不能转换为int
有人知道我做错了什么吗? Maven 编译器插件似乎是最新的,我的 JAVA_HOME 指向 Java 8 版本。
pom.xml 中的片段
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<showWarnings>false</showWarnings>
<showDeprecation>false</showDeprecation>
</configuration>
</plugin>
配置java.version为1.8; Maven 是 3.0.4。
最佳答案
我遇到了和你一样的错误。但是下面的代码修复了它。我已将代码设为方法而不是构造函数,但我认为您可以忽略它,嗯?
private String weather;
private double temperature;
private int humidity;
public void WeatherDTO(Map<?, ?> mappedJsonData) {
if (mappedJsonData == null || mappedJsonData.isEmpty()) {
return;
}
List<?> weather = (List<?>) mappedJsonData.get("weather");
if (weather != null && !weather.isEmpty()) {
this.weather = (String) ((Map<?, ?>) weather.get(0)).get("description");
}
Map<?, ?> jsonMain = (Map<?, ?>) mappedJsonData.get("main");
if (jsonMain != null && !jsonMain.isEmpty()) {
this.temperature = (Double) jsonMain.get("temp") - 273.0;
this.humidity = (Integer) jsonMain.get("humidity");
}
}
关于java - 使用 Java 泛型的 Maven 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31860761/