简体   繁体   English

使用打开的 api 代码生成器为 spring-webflux 生成 API 接口时,如何防止 Mono 请求正文生成?

[英]How to prevent Mono Request body generation when generating the API interfaces for spring-webflux with open api code generator?

I'm creating a spring boot application using spring-webflux and generating the api's using open api code generator.我正在使用 spring-webflux 创建一个 spring 引导应用程序,并使用打开的 api 代码生成器生成 api。 the generated interfaces wrappes the request body with Mono.生成的接口用 Mono 包装请求体。 How to prevent that wrapping?如何防止这种包装? for example instead of generating例如,而不是生成

public Mono<ResponseEntity<PostApiDto>> createPost(Mono<PostApiDto> body, ServerWebExchange exchange) {

}

i want the generated interface to look like:我希望生成的界面看起来像:

public Mono<ResponseEntity<PostApiDto>> createPost(PostApiDto body) {

}

Here is the api spec file and the pom.xml file:-这是 api 规范文件和 pom.xml 文件:-

openapi.yml openapi.yml

openapi: 3.0.3
info:
  title: "instagram-post-service"
  description: ""
  termsOfService: ""
  version: 1.0.0
externalDocs:
  description: ""
  url: ""
servers:
  - url: http://localhost:8080
    description: Generated server url
tags:
  - name: Post
    description: Operations about posts
    externalDocs:
      description: ""
      url: ""
  - name: Comment
    description: Operations about comments
    externalDocs:
      description: ""
      url: ""
    
paths:
  /posts/me/:
    get:
      tags:
      - Post
      summary: find current user posts
      operationId: getMyPosts
      responses:
        200:
          description: successful operation
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Post'
  /posts/:                  
    post:
      tags:
        - Post
      summary: Create a new Post
      description: ''
      operationId: createPost
      requestBody:
        description: ''
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Post'
        required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Post'          


components:
  schemas:
    Post:
      type: object
      properties:
        id:
          type: integer
          format: int32
        title:
          type: string
        serviceAddress:
          type: string      

pom.xml pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.javaworld.instagram</groupId>
    <artifactId>post-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>post-service</name>
    <description>instagram posts micro service</description>

    <properties>
        <java.version>1.8</java.version>
        <swagger-annotations-version>1.6.6</swagger-annotations-version>
        <jackson-databind-nullable>0.2.1</jackson-databind-nullable>
        <org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
    </properties>

    <dependencies>
        
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
                
        <!-- ################### Testing dependencies ################################ -->  
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
        </dependency>       
        
        <!-- ########################### DB & persistence layer dependencies ################################ -->

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
                
    
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.1.2.Final</version>
        </dependency>
    

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
        </dependency>
        
        <!-- ################### dependencies needed for open-API code generator ################################ -->
        
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>${swagger-annotations-version}</version>
        </dependency>

        <dependency>
            <groupId>org.openapitools</groupId>
            <artifactId>jackson-databind-nullable</artifactId>
            <version>${jackson-databind-nullable}</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>


        <!-- ################################ MapStruct ################################################ -->

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layers>
                        <enabled>true</enabled>
                        <!-- <configuration>${project.basedir}/src/layers.xml</configuration> -->
                    </layers>
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source> 
                    <target>1.8</target> 
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                        <!-- other annotation processors -->
                    </annotationProcessorPaths>
                </configuration>
            </plugin>

            <!-- open API plug-in for API code generator -->
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>6.0.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${project.basedir}/src/main/resources/openapi.yml
                            </inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>com.javaworld.instagram.postservice.server.api</apiPackage>
                            <modelPackage>com.javaworld.instagram.postservice.server.dto</modelPackage>
                            <modelNameSuffix>ApiDto</modelNameSuffix>
                            <configOptions>
                                <interfaceOnly>true</interfaceOnly>
                                <skipDefaultInterface>true</skipDefaultInterface>
                                <reactive>true</reactive>
                            </configOptions>

                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

You need to customize your own swagger-codegen template files ( api.mustache and bodyParams.mustache ) and add them to your project.您需要自定义自己的 swagger-codegen 模板文件( api.mustachebodyParams.mustache )并将它们添加到您的项目中。

Your pom.xml config你的 pom.xml 配置

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>6.0.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${project.basedir}/src/main/resources/openapi.yml
                            </inputSpec>
                            <templateDirectory>src/main/resources/openapi-templates</templateDirectory>
                            <generatorName>spring</generatorName>
                            <apiPackage>com.javaworld.instagram.postservice.server.api</apiPackage>
                            <modelPackage>com.javaworld.instagram.postservice.server.dto</modelPackage>
                            <modelNameSuffix>ApiDto</modelNameSuffix>
                            <configOptions>
                                <interfaceOnly>true</interfaceOnly>
                                <skipDefaultInterface>true</skipDefaultInterface>
                                <reactive>true</reactive>
                            </configOptions>

                        </configuration>
                    </execution>
                </executions>
            </plugin>

templateDirectory - directory with mustache templates api.mustache and bodyParams.mustache templateDirectory - 带有胡子模板的目录api.mustachebodyParams.mustache

Here you can find modified mustache template files for your case. 在这里,您可以找到适用于您的案例的修改后的 mustache 模板文件。

Output Output

default Mono<ResponseEntity<PostApiDto>> createPost(@Parameter(name = "",required = true) @RequestBody @Valid PostApiDto postApiDto, @Parameter(hidden = true) ServerWebExchange exchange) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spring-webflux url重定向和静态html和rest api路由 - spring-webflux url redirect and static html and rest api routing 如何将@RestController 添加到 spring-webflux 应用程序? - How to add @RestController to spring-webflux apps? Spring 解码时启动 Webflux 处理程序错误 XML 将正文内容请求到 Mono 或 Flux object - Spring Boot Webflux Handler errors when decoding XML request body content to Mono or Flux object spring webflux:如何发送Mono <T> 在身体插入器的反应体 - spring webflux: how to send Mono<T> in response body with body inserters 如何记录 spring-webflux WebClient 请求 + 响应详细信息(主体、标题、elasped_time)? - How to log spring-webflux WebClient request + response details (bodies, headers, elasped_time)? 如何在 spring Webflux Java 中记录请求正文 - How to log request body in spring Webflux Java 如何在没有spring-boot的情况下在spring-webflux中加载配置? - how to load config in spring-webflux without spring-boot? @EnableResourceServer不适用于spring-webflux - @EnableResourceServer not working with spring-webflux 使用spring-webflux时WebTestClient不起作用 - WebTestClient does not work when using spring-webflux 如何在 spring-webflux WebTestClient 测试中验证异常? - How to validate Exception in spring-webflux WebTestClient tests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM