[英]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.mustache
和bodyParams.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.mustache
和bodyParams.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.