将Swagger Codegen生成的服务器存根插入现有的Spring MVC应用程序的最佳方法是什么?
我开始尝试使用petstore stubs样本。
我的Spring配置是用Java编写的,如下所示:
public class SpringConfigurationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { ApplicationContext.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebMvcContext.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
// ... onStartup etc.
}
WebMvcConfigurationSupport:
@Configuration
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
@PropertySource({ "classpath:config.properties", "file:${CONFIGDIR}/config.properties" })
@ComponentScan(useDefaultFilters = false, basePackages = { "com.yyy", "com.xxx" }, includeFilters = { @Filter(type = FilterType.ANNOTATION, value = Controller.class) })
public class WebMvcContext extends WebMvcConfigurationSupport {
// ... beans etc.
}
ApplicationContext的:
@Configuration
@EnableAsync
@EnableScheduling
@EnableMBeanExport
@Import({SecurityConfig.class, GeneralDBConfiguration.class})
@ComponentScan(useDefaultFilters = true, basePackages = { "com.yyy", "com.xxx" }, excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = {Controller.class, Configuration.class/*, Aspect.class*/}) })
public class ApplicationContext implements AsyncConfigurer {
// beans etc.
}
如何将io.swagger.configuration
包的配置类部分包含到我现有的应用程序中?
更多细节:
我遇到的一个问题是,如果我在petshop存根上指定了一个maven依赖项(通过从mvn install:install-file ...
目录运行spring-mvc-j8-async
来本地安装):
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-spring-mvc-server</artifactId>
<version>1.0.0</version>
</dependency>
然后我的春季应用程序找到两个AbstractAnnotationConfigDispatcherServletInitializer
s(一个来自我的应用程序,io.swagger.configuration.WebApplication
一个来自swagger-spring-mvc-server
)并且无法加载 - 给出以下异常:
无法注册名为'dispatcher'的servlet。检查是否有另一个以相同名称注册的servlet。
我想用另一种方式来表达我的问题是,你如何使用swagger-codegen
生成的服务器存根?看起来我不能仅仅依赖于开箱即用的maven包......
我可以看到这是一个很旧的,但我很努力地解决这个问题,我收集的信息可能对其他人有用,直到生成器文档得到改进。
此描述用于使用openapi-generator-maven-plugin从OpenApi 3.0.0规范生成和使用spring-mvc服务器存根。
<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>3.3.4</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<skipIfSpecIsUnchanged>true</skipIfSpecIsUnchanged>
<inputSpec>${engine-openapi-spec.location}</inputSpec>
<output>${project.build.directory}/generated-sources/openapi</output>
<generatorName>spring</generatorName>
<library>spring-mvc</library>
<apiPackage>eu.dorsum.swift.engine.service.api</apiPackage>
<modelPackage>eu.dorsum.swift.engine.service.model</modelPackage>
<generateApis>true</generateApis>
<generateApiDocumentation>false</generateApiDocumentation>
<generateApiTests>false</generateApiTests>
<generateModels>true</generateModels>
<generateModelDocumentation>false</generateModelDocumentation>
<generateModelTests>false</generateModelTests>
<generateSupportingFiles>true</generateSupportingFiles>
<configOptions>
<sourceFolder>src/main/java</sourceFolder>
<java8>true</java8>
<dateLibrary>java8</dateLibrary>
<useTags>true</useTags>
<configPackage>eu.dorsum.swift.engine.appconfig</configPackage>
<interfaceOnly>false</interfaceOnly>
<delegatePattern>true</delegatePattern>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
有两个配置,有点难以弄清楚。
2.a:将configOptions/configPackage
设置为应用程序根配置所在的包。此选项将您的配置包作为附加的basePackage添加到OpenAPIUiConfiguration.java中的组件扫描:@ComponentScan(basePackages = {"eu.dorsum.swift.engine.service.api", "eu.dorsum.swift.engine.appconfig"})
。这是一个倒置的方法,你可能会想到最初生成的mvc配置启动你现有的东西,但这是我发现不需要修改生成的代码的唯一方法。
2.b:将configOptions/delegatePattern
设置为true。这个我很喜欢!这将生成服务器控制器可以实现的附加委派接口。生成的ApiController会将所有服务调用委托给此接口,因此您可以非常优雅地插入您的实现。在我的设置中我有这个链:MessageApi(生成的接口) - > MessageApiController实现MessageApi(生成的mvc控制器) - > MessageApiDelegate(生成的接口) - > MessageService实现MessageApiDelegate(我的服务方法的实现)。
拥有这两个配置后,无需修改生成的源。如果api spec更改了委托接口更改,则必须在MessageService中实现这些更改。