OpenAPI / Swagger文档中未显示Apache Camel REST发布主体参数

j8ag8udp  于 2023-03-18  发布在  Apache
关注(0)|答案(1)|浏览(212)

我正在尝试使用ApacheCamel(v3.20.0)设置REST服务,该服务提供了一个POST端点,该端点接收请求主体中的有效负载。
我尝试使用camel-openapi-java包和Java DSL来记录REST接口,但是,当我尝试使用.param().name("body").type(body).description("The post object that is to be published").endParam()指定所需的body参数时,该参数没有添加到生成的OpenAPI 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");

生成的Open API 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":{}}}

当我试图声明RestParamType.path和RestParamType.query参数时,它对这些参数都能很好地工作,但是任何RestParamType.body类型的参数都会被忽略。
Post.java预期作为POST请求主体中的有效载荷,实现为:

@Data
@NoArgsConstructor
@Schema(description = "Post Object as expected by the service")
public class Post {

    @Schema(
            name="id",
            description = "ID by which post is stored in local database",
            example="42",
            implementation = Integer.class,
            required = true
    )
    Integer id; // as stored in the posts database

    @Schema(
            name="partnerId",
            description = "ID of partner on behalf of which the post is to be published",
            example="42",
            implementation = Integer.class,
            required = true
    )
    Integer partnerId ; // as stored in partners table

    @Schema(
            name="textContent",
            description = "Text content of the post.",
            example="This is a descriptive comment of the post",
            implementation = String.class,
            required = true
    )
    String textContent ;

    @Schema(
            name="type",
            description = "Type of the post. Determines the expected type of media declared for the post.",
            example="VIDEO",
            implementation = PostType.class,
            required = true
    )
    PostType type;

    @ArraySchema
    @Schema(
            name="media",
            description = "Media objects declared for the post. Single element expected for IMAGE, and VIDEO. More than one element expected for MULTI_IMAGE",
            required = true)
    SpMedia[] media ;
}

pom.xml:

<properties>
        <camel.version>3.20.0</camel.version>
        <java.version>17</java.version>
        <spring-cloud.version>2021.0.3</spring-cloud.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-test-spring-junit5</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-rest-starter</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-servlet-starter</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-jackson-starter</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-http-starter</artifactId>
            <version>${camel.version}</version>
        </dependency>

        <!-- Swagger and OpenAPI dependencies -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-openapi-java</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-springdoc-starter</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
        </dependency>

要将请求正文参数正确地包含到OpenAPI文档中,我遗漏了什么?

eagi6jfj

eagi6jfj1#

您需要为正文有效负载提供预期的媒体类型。您可以在Camel REST DSL中使用.consumes()方法完成此操作。例如:

rest().description(serviceConfig.getServiceName(), serviceConfig.getServiceName(), "en")
  .post("/post")
  .description(serviceConfig.getServiceName(), "Receives a post to be published", "en")
  .type(Post.class)
  .consumes("application/json")
  .param().name("body").type(body).description("The post that is to be published.").endParam()
  .to("direct:servicePipeline");

相关问题