如何正确使用从swagger规范生成的服务器存根? [英] How to properly use the server stubs generated from a swagger specification?

查看:207
本文介绍了如何正确使用从swagger规范生成的服务器存根?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Swagger 2.0和swagger-codegen(实际上是Maven的swagger-codegen-plugin)来指定,记录和生成一个API,使用Java作为目标语言。

I'm using Swagger 2.0 and swagger-codegen (actually the swagger-codegen-plugin for Maven) to specify,document and generate an API, with Java as the target language.

该项目已经安装构建服务器存根(JAX-RS)和文档,Eclipse识别项目buildPath中生成的代码。

The project is already setup to build the server stubs (JAX-RS) and documentation, and Eclipse recognizes the generated code in the project buildPath.

I 不知道从这里是什么正确的工作流程。 : - /

I'm not sure of what is the proper workflow from here. :-/

我不认为我应该修改生成的类,否则当我更改swagger规范时,我的更改将被覆盖,我期望它会改变为随着开发的进行,我更多地关注API。

I don't think I should modify the generated classes, otherwise my changes would be overwritten whenever I change the swagger spec, an I expect it will change as I think more about the API as the development goes on.

那么该怎么办?继承自生成的类(哪些?)或将它们包含在我自己的类中?

What should I do then? Inherit from the generated classes (which ones?) or include them in my own classes?

推荐答案

解决方案有两个步骤这里。

There are two steps to the solution here.


  1. 将** / * Controller.java或** / * Impl.java添加到.swagger-codegen-ignore文件。根据所使用的语言,默认实现在* Controller.java或* Impl.java文件中提供。一旦默认实现被排除在一代之外,就可以在自己的类中实现生成的接口。您自己的类中的代码将不会在mvn clean中刷新。

  1. Add **/*Controller.java or **/*Impl.java to .swagger-codegen-ignore file. Depending on the language used the default implementation is provided in a *Controller.java or *Impl.java file. Once the default implementation is excluded from generation, you can implement the generated interfaces in your own class. The code in your own class will not get refreshed on mvn clean.

.swagger-codegen-ignore文件本身是一个自动生成的文件,因此无论你添加什么当您执行mvn清理时,在步骤1中刷新。为了避免这种情况在您的资源文件夹中保留您的 .swagger-codegen-ignore 的版本,并将以下插件添加到您的pom,以在Maven生命周期开始时复制该文件:

.swagger-codegen-ignore file itself is an auto-generated file hence whatever you add in step 1 gets refreshed when you do a mvn clean. To avoid this keep your version of .swagger-codegen-ignore in your resources folder and add the below plugin to your pom, to copy the file at the start of the Maven lifecycle:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<executions>
		<execution>
			<id>copy-resources</id>
			<phase>initialize</phase>
			<goals>
				<goal>copy-resources</goal>
			</goals>
			<configuration>
				<outputDirectory>${project.build.directory}/generated/swagger</outputDirectory>
				<resources>
					<resource>
						<directory>${basedir}/src/main/resources</directory>
						<includes>
							<include>.swagger-codegen-ignore</include>
						</includes>
					</resource>
				</resources>
			</configuration>
		</execution>
	</executions>
</plugin>

这篇关于如何正确使用从swagger规范生成的服务器存根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆