使用开放式API代码生成器为spring-webflux生成API接口时,如何防止Mono-Request主体生成



我正在使用spring-webflux创建一个spring-boot应用程序,并使用打开api代码生成器。生成的接口用Mono封装请求主体。如何防止这种包裹?例如,而不是生成

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

我希望生成的界面看起来像:

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

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

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


<?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>

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

您的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-带有胡子模板的目录api.mustachebodyParams.mustache

在这里你可以找到修改后的胡子模板文件为你的情况。

输出

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

最新更新