Swagger Codegen不生成模型及LocalTime类型解析报错求助
问题1:swagger-codegen-maven-plugin(2.4.30)未生成DTO模型
已配置modelPackage为com.swagger.client.codegen.rest.model,但仅生成api和invoker包、无模型类,解决步骤如下:
- 检查api.json的schemas结构:确保
components/schemas下存在有效的模型定义,每个模型需包含type(如object)、properties等必填字段,空的或格式错误的schemas不会触发模型生成 - 补全插件配置:确认插件指定了正确的
generatorName(如java),并显式开启模型生成:<configuration> <!-- 其他已有配置 --> <generateModels>true</generateModels> <generatorName>java</generatorName> </configuration> - 清理缓存重新构建:执行
mvn clean compile,避免旧构建缓存干扰 - 查看Debug日志定位问题:运行
mvn swagger-codegen:generate -X,通过日志检查是否存在schemas解析错误、模型生成被跳过的提示信息
问题2:openapi-generator-maven-plugin(4.2.3)生成代码出现org.joda.time.*无法解析
生成的模型类中localTime字段使用org.joda.time.*通配符导致编译错误,解决方法如下:
原因
OpenAPI规范中时间类型定义不明确,或插件日期时间库配置错误,导致生成代码时无法映射到具体的JodaTime类。
解决步骤
- 明确OpenAPI规范中的时间类型
在api.json的schemas里,将localTime字段的类型明确为string并指定local-time格式:"components": { "schemas": { "EventDtoZoneIdRulesTransitionRules": { "properties": { "localTime": { "type": "string", "format": "local-time" } } } } } - 调整插件的日期库配置
推荐改用Java 8内置的java.time包替代JodaTime,在插件的configOptions中添加:
若必须使用JodaTime,需指定正确的日期库配置:<configOptions> <delegatePattern>true</delegatePattern> <dateLibrary>java8</dateLibrary> <java8>true</java8> </configOptions><configOptions> <delegatePattern>true</delegatePattern> <dateLibrary>joda</dateLibrary> </configOptions> - 添加依赖(若保留JodaTime)
如果继续使用JodaTime,需在pom.xml中添加依赖:<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.12.5</version> </dependency> - 清理并重新生成代码
执行mvn clean openapi-generator:generate,确保旧代码被清理,重新生成正确的类型引用
内容的提问来源于stack exchange,提问作者user19751568
相关产品推荐
相关产品推荐

