使用OpenAPI生成Micronaut POST端点时构建失败求助
问题
使用openapi-generator结合Micronaut开发时,构建抛出NullPointerException:
Cannot invoke "String.equals(Object)" because the return value of "io.swagger.v3.oas.models.parameters.Parameter.getIn()" is null
完整报错堆栈:
warning: Error: Cannot invoke "String.equals(Object)" because the return value of "io.swagger.v3.oas.models.parameters.Parameter.getIn()" is null java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because the return value of "io.swagger.v3.oas.models.parameters.Parameter.getIn()" is null at io.micronaut.openapi.visitor.SchemaUtils.mergeOperations(SchemaUtils.java:324) ...(完整堆栈省略)
相关配置及代码如下:
OpenAPI规范
openapi: 3.0.0 info: version: 1.0.0 title: Ninja tags: - name: rentals paths: /rentals: post: tags: - rentals operationId: create requestBody: content: "application/json": schema: $ref: "#/components/schemas/RentalInfo" responses: "201": description: Created content: "application/json": schema: $ref: "#/components/schemas/RentalInfo" "400": description: Bad Request components: schemas: RentalInfo: type: object properties: uuid: { type: string } name: { type: string } required: [ "uuid", "name" ]
API实现代码
@Controller class Rentals implements RentalsApi { @Override public HttpResponse<RentalInfo> create(final RentalInfo rentalInfo) { return HttpResponse.created(rentalInfo); } }
Gradle配置
plugins { id("groovy") id("com.github.johnrengelman.shadow") version "8.1.1" id("io.micronaut.application") version "4.0.4" id("io.micronaut.aot") version "4.0.4" id("io.micronaut.openapi") version "4.0.4" } version = "0.1" group = "dev.jjrz" repositories { mavenCentral() } dependencies { annotationProcessor("io.micronaut:micronaut-http-validation") annotationProcessor("io.micronaut.openapi:micronaut-openapi") annotationProcessor("io.micronaut.serde:micronaut-serde-processor") implementation("io.micronaut.security:micronaut-security") implementation("io.micronaut.serde:micronaut-serde-jackson") implementation("io.swagger.core.v3:swagger-annotations") compileOnly("io.micronaut:micronaut-http-client") runtimeOnly("ch.qos.logback:logback-classic") testImplementation("io.micronaut:micronaut-http-client") } application { mainClass.set("dev.jjrz.ninja2.Application") } java { sourceCompatibility = JavaVersion.toVersion("17") targetCompatibility = JavaVersion.toVersion("17") } graalvmNative.toolchainDetection = false micronaut { runtime("netty") testRuntime("spock2") processing { incremental(true) annotations("dev.jjrz.*") } aot { optimizeServiceLoading = false convertYamlToJava = false precomputeOperations = true cacheEnvironment = true optimizeClassLoading = true deduceEnvironment = true optimizeNetty = true } openapi { server(file("src/main/resources/ninja2-api.yml")) { apiPackageName = "dev.jjrz.ninja2.api" modelPackageName = "dev.jjrz.ninja2.model" useReactive = false useAuth = true } } }
已知移除请求体或去掉@Controller注解时构建可正常通过,但@Controller是端点实现必需的,需解决该问题以实现POST请求的正常生成。
解决方案
给请求体参数添加@Body注解
在API实现的create方法参数上显式标记@Body注解,明确该参数对应请求体,避免Micronaut处理元数据时将其误判为无in属性的参数:
@Controller class Rentals implements RentalsApi { @Override public HttpResponse<RentalInfo> create(@Body final RentalInfo rentalInfo) { return HttpResponse.created(rentalInfo); } }
升级Micronaut OpenAPI插件版本
该NullPointerException是Micronaut OpenAPI插件4.0.x版本的已知bug,升级到4.1.0及以上版本可修复。修改Gradle配置中的插件版本:
plugins { // 保留其他插件配置 id("io.micronaut.openapi") version "4.1.0" }
同时同步更新依赖中的注解处理器版本,确保与插件版本一致:
dependencies { // 保留其他依赖配置 annotationProcessor("io.micronaut.openapi:micronaut-openapi:4.1.0") }
在OpenAPI规范中显式标记请求体为必填
在OpenAPI的requestBody节点添加required: true,帮助插件更准确识别请求体参数:
paths: /rentals: post: # 保留其他配置 requestBody: required: true content: "application/json": schema: $ref: "#/components/schemas/RentalInfo"
内容的提问来源于stack exchange,提问作者jjrz
相关产品推荐
相关产品推荐

