Apache Camel POST请求体参数未在OpenAPI文档中显示的问题排查
问题描述
使用Apache Camel v3.20.0搭建REST服务,POST端点需要接收请求体负载,通过Java DSL结合camel-openapi-java生成OpenAPI文档时,请求体参数(RestParamType.body)未出现在生成的JSON中,路径和查询参数可正常显示。
现有配置代码
REST路由定义
rest().description(serviceConfig.getServiceName(), serviceConfig.getServiceName(), "en") .post("/post") .description(serviceConfig.getServiceName(), "Receives a post to be published", "en") .type(Post.class) .param().name("body").type(body).description("The post that is to be published.").endParam() .to("direct:servicePipeline");
Rest全局配置
restConfiguration() .component("servlet") .host("0.0.0.0") .port(8080) .bindingMode(RestBindingMode.json) .contextPath(serviceConfig.getVersionPath() + "/" + serviceConfig.getAffordancePath()) .enableCORS(true) .apiContextPath("docs") .apiProperty("api.title", serviceConfig.getServiceName()) .apiProperty("api.version", "1.0") .apiProperty("cors", "true");
生成的OpenAPI JSON(缺失请求体)
{"openapi":"3.0.2","info":{"version":"1.0"},"servers":[{"url":"/api/v1/organicposts"}],"paths":{"/post":{"post":{"summary":"Receives a post to be published","operationId":"verb1","responses":{"200":{}}}}},"components":{"schemas":{}}}
解决方案
移除冗余的body参数声明
Camel中.type(Post.class)已经明确指定了请求体的类型,无需再通过.param().name("body")...重复声明。这种显式声明会被OpenAPI生成器忽略,还可能干扰自动识别逻辑。添加Schema自动扫描配置
在restConfiguration中补充以下配置,让Camel自动扫描并生成请求体对应的Schema:
.apiProperty("scan", "true") .apiProperty("scan.packages", "com.your.package.where.Post.class.is.located")
将com.your.package.where.Post.class.is.located替换为Post类实际所在的包路径,配置后生成的OpenAPI文档会自动包含Post的Schema定义,并将其绑定为POST接口的请求体类型。
- 验证依赖版本匹配
确保pom.xml中camel-openapi-java依赖版本与Camel v3.20.0完全一致:
<dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-openapi-java-starter</artifactId> <version>3.20.0</version> </dependency>
调整后的REST路由
rest().description(serviceConfig.getServiceName(), serviceConfig.getServiceName(), "en") .post("/post") .description(serviceConfig.getServiceName(), "Receives a post to be published", "en") .type(Post.class) .to("direct:servicePipeline");
完成以上配置后,生成的OpenAPI文档会自动包含请求体定义及对应的Post类Schema。
内容的提问来源于stack exchange,提问作者Torsten S
相关产品推荐
相关产品推荐

