升级JDK11与enunciate插件后,泛型类编译失败求助
解决Enunciate 2.15.x生成Java JSON客户端时泛型被替换为
?的编译错误 问题场景
维护的遗留项目从Java 8 + Enunciate Maven插件2.13.0升级到JDK 11 + Enunciate 2.15.1后,执行docs goal时出现Java JSON客户端类编译失败。父POM中jax-rs-api profile配置如下:
<profile> <id>jax-rs-api</id> <dependencies> <dependency> <groupId>com.webcohesion.enunciate</groupId> <artifactId>enunciate-core-annotations</artifactId> <version>${enunciate-maven-plugin.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>com.webcohesion.enunciate</groupId> <artifactId>enunciate-maven-plugin</artifactId> <executions> <execution> <goals> <goal>docs</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile>
涉及的抽象DTO类:
public abstract class IdentityView<T> { private String accountId; private T profile; private ConfigurationView configuration; // setters & getters }
编译错误显示生成的客户端代码中泛型T被错误替换为?,导致语法错误:
C:\...\my-project\target\enunciate\java-json-client\src\com\my-project\data\view\IdentityView.java:21: error: illegal start of type private ? _profile; ^ C:\...\my-project\target\enunciate\java-json-client\src\com\my-project\data\view\IdentityView.java:21: error: <identifier> expected private ? _profile; ^ C:\...\my-project\target\enunciate\java-json-client\src\com\my-project\data\view\IdentityView.java:45: error: illegal start of type public ? getProfile() { ^ C:\...\my-project\target\enunciate\java-json-client\src\com\my-project\data\view\IdentityView.java:45: error: invalid method declaration; return type required public ? getProfile() { ^ C:\...\my-project\target\enunciate\java-json-client\src\com\my-project\data\view\IdentityView.java:53: error: illegal start of type public void setProfile(? _profile) {
解决思路
显式指定泛型边界或添加Enunciate注解:Enunciate对无边界泛型的类型推断可能存在问题,给
T添加边界约束(如T extends BaseProfile),或者添加Enunciate的@JsonTypeInfo注解明确类型信息:import com.webcohesion.enunciate.metadata.json.JsonTypeInfo; @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@type") public abstract class IdentityView<T> { // 原有代码 }调整Enunciate插件配置:如果不需要Java JSON客户端,直接禁用该模块:
<plugin> <groupId>com.webcohesion.enunciate</groupId> <artifactId>enunciate-maven-plugin</artifactId> <executions> <execution> <goals> <goal>docs</goal> </goals> </execution> </executions> <configuration> <modules> <java-json-client> <disabled>true</disabled> </java-json-client> </modules> </configuration> </plugin>若需保留客户端,可尝试配置泛型处理参数,比如指定
genericTypes来明确泛型映射。升级Enunciate版本:2.15.x版本可能存在泛型处理的bug,升级到最新稳定版本(如2.18.0+),官方可能已修复此类问题。
配置JDK编译兼容参数:确保Enunciate使用JDK 11的编译参数,在插件配置中添加:
<plugin> <groupId>com.webcohesion.enunciate</groupId> <artifactId>enunciate-maven-plugin</artifactId> <executions> <execution> <goals> <goal>docs</goal> </goals> </execution> </executions> <configuration> <compileArgs> <arg>-source</arg> <arg>11</arg> <arg>-target</arg> <arg>11</arg> </compileArgs> </configuration> </plugin>处理抽象类的泛型识别:确保
IdentityView的子类被Enunciate扫描到,或者给抽象类添加泛型实例化的提示,帮助Enunciate正确识别泛型类型。
内容的提问来源于stack exchange,提问作者ChambreNoire
相关产品推荐
相关产品推荐

