如何将由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.
}
应用上下文:
@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
包中的配置类部分包含到我的现有应用程序中?
更多细节如下:
我遇到的问题之一是,如果我在我的应用程序中指定了一个maven依赖项来使用宠物店存根(通过从spring-mvc-j8-async
目录运行mvn install:install-file ...
安装到本地):
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-spring-mvc-server</artifactId>
<version>1.0.0</version>
</dependency>
然后我的Spring应用程序找到了两个AbstractAnnotationConfigDispatcherServletInitializer(一个来自我的应用程序,另一个来自swagger-spring-mvc-server中的io.swagger.configuration.WebApplication),并且无法加载它们-导致以下异常:
无法使用名称'dispatcher'注册servlet。检查是否有另一个已注册相同名称的servlet。
我猜另一种表达我的问题的方式是,如何使用swagger-codegen
生成的服务器存根?看起来我不能直接依赖于开箱即用的maven软件包...
spring-mvc-j8-async
存根--实际上,io.swagger.configuration
包与“普通”的spring-mvc
存根中的一个完全相同。 - jlb