首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在用开放的API代码生成器为spring生成api接口时防止Mono请求体生成?

如何在用开放的API代码生成器为spring生成api接口时防止Mono请求体生成?
EN

Stack Overflow用户
提问于 2022-09-05 21:40:38
回答 1查看 290关注 0票数 0

我正在使用spring创建一个spring引导应用程序,并使用开放的api代码生成器生成api。生成的接口用Mono包装请求体。如何防止包裹?例如,而不是生成

代码语言:javascript
运行
复制
public Mono<ResponseEntity<PostApiDto>> createPost(Mono<PostApiDto> body, ServerWebExchange exchange) {

}

我希望生成的接口如下所示:

代码语言:javascript
运行
复制
public Mono<ResponseEntity<PostApiDto>> createPost(PostApiDto body) {

}

下面是api规范文件和pom.xml文件:-

openapi.yml

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-06 10:13:51

您需要自定义自己的swagger-codegen模板文件(api.mustachebodyParams.mustache),并将它们添加到项目中。

您的pom.xml配置

代码语言:javascript
运行
复制
            <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 -带有胡子模板的目录api.mustachebodyParams.mustache

这里,您可以为您的情况找到修改过的胡子模板文件。

输出

代码语言:javascript
运行
复制
default Mono<ResponseEntity<PostApiDto>> createPost(@Parameter(name = "",required = true) @RequestBody @Valid PostApiDto postApiDto, @Parameter(hidden = true) ServerWebExchange exchange) {}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73614880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档