使用Java泛型时出现Maven编译错误
Im使用Java 8并具有以下代码:使用Java泛型时出现Maven编译错误,java,maven,generics,Java,Maven,Generics,Im使用Java 8并具有以下代码: public WeatherDTO(Map<?,?> mappedJsonData) { if (mappedJsonData == null || mappedJsonData.isEmpty()) { return; } List<?> weather = (List<?>)mappedJsonData.get("weather"); if (weather != nu
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");
}
}
public WeatherDTO(地图mappedJsonData){
if(mappedJsonData==null | | mappedJsonData.isEmpty()){
返回;
}
List weather=(List)mappedJsonData.get(“天气”);
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.湿度=(int)jsonMain.get(“湿度”);
}
}
当我在Eclipse和嵌入式Tomcat中运行web应用程序时,此代码运行良好。Eclipse问题视图中也没有显示任何问题
但是,当我从shell/bash启动maven编译时,我得到以下错误:
[错误]编译错误:
[信息]-------------------------------------------------------------
[错误]/home/xxx/xxx/xxx/src/main/java/xxx/xxx/xxx/weather/WeatherDTO.java:[52,64]不兼容类型:捕获#1个?不能转换为双精度
[错误]/home/xxx/xxx/xxx/src/main/java/xxx/xxx/xxx/weather/WeatherDTO.java:[53,58]不兼容类型:捕获#2个?无法转换为int
有人知道我做错了什么吗?
maven编译器插件似乎是最新的,我的JAVA_主页指向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>
真的
org.apache.maven.plugins
maven编译器插件
3.3
${java.version}
${java.version}
${project.build.sourceEncoding}
假的
假的
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");
}
}
私人字符串天气;
私人双温;
私人室内湿度;
公共空间天气图(地图mappedJsonData){
if(mappedJsonData==null | | mappedJsonData.isEmpty()){
返回;
}
List weather=(List)mappedJsonData.get(“天气”);
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.湿度=(整数)jsonMain.get(“湿度”);
}
}
它使用对象而不是基本类型。我会使用(双精度)
和(整数)
。但是,使用泛型和到处都有通配符和强制转换非常奇怪。我从一个示例中学习了如何使用jackson转换/映射json数据。这可能是一个错误,因为我在使用泛型方面不是很强…试试((double)jsonMain.get(“temp”)-273.0
。你的jsonMain
是一个Map
,实际上是Map
。当您调用jsonMain.get(“temp”)
时,它将向您返回一个对象
,您无法将其转换为双精度
。不过,您可能会将其转换为双精度。Jackson不仅仅是一个JSON解析器。它也是一个JSON到Java对象映射器。您应该定义与JSON具有相同结构的Java对象,并使用类型安全代码。将其转换为对象而不是原始类型确实可以修复它。但仍然不知道为什么只有Maven compile有问题,而不是我的ide。奇怪的是。。。。