1. 搜索 spring showcase
就可以找到这篇 http://spring.io/blog/2010/07/22/spring-mvc-3-showcase
就是教你如何使用spring mvc
2. 去github 下载源码
3. 接下来的任务就是将swagger 整合进去
--------------------------------------------------------------------------
1. 进入 https://github.com/springfox/springfox/blob/v1.0.2/README.md 跟着一步一步做
2. 加入 依赖
<dependency> <groupId>com.mangofactory</groupId> <artifactId>swagger-springmvc</artifactId> <version>1.0.1</version> </dependency>
读到这里发现还少包,意思是swagger-springmvc 在 0.9.5 版的时候移除了 scala 的全部依赖,还需要Spring 3.2.X 或以上,需要jackson 2.4.4或以上,需要guava 15.0 或以上 。
Notable Dependencies
As of v0.9.5 all dependencies on scala have been removed.
Spring 3.2.x or above
jackson 2.4.4
guava 15.0
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.4</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>15.0</version> </dependency>
随后发现缺少spring web 包 ,然后引入
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework-version}</version> </dependency>
至此引入全部所需包。
3. 随后按照配置运行过程中,发现一个问题
六月 03, 2016 6:41:05 下午 org.apache.catalina.core.StandardContext loadOnStartup
严重: Servlet /spring-mvc-showcase threw load() exception
java.lang.IllegalArgumentException: Conflicting setter definitions for property "content": org.jdom2.Element#setContent(1 params) vs org.jdom2.Element#setContent(1 params)
at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getSetter(POJOPropertyBuilder.java:293)
at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getMutator(POJOPropertyBuilder.java:376)
at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getPrimaryMember(POJOPropertyBuilder.java:398)
at com.mangofactory.swagger.models.property.field.FieldModelPropertyProvider.propertiesForDeserialization(FieldModelPropertyProvider.java:85)
at com.mangofactory.swagger.models.property.provider.DefaultModelPropertiesProvider.propertiesForDeserialization(DefaultModelPropertiesProvider.java:43)
at com.mangofactory.swagger.models.ModelDependencyProvider.propertiesFor(ModelDependencyProvider.java:121)
at com.mangofactory.swagger.models.ModelDependencyProvider.resolvedPropertiesAndFields(ModelDependencyProvider.java:94)
at com.mangofactory.swagger.models.ModelDependencyProvider.resolvedDependencies(ModelDependencyProvider.java:75)
at com.mangofactory.swagger.models.ModelDependencyProvider.resolvedPropertiesAndFields(ModelDependencyProvider.java:108)
at com.mangofactory.swagger.models.ModelDependencyProvider.resolvedDependencies(ModelDependencyProvider.java:75)
at com.mangofactory.swagger.models.ModelDependencyProvider.dependentModels(ModelDependencyProvider.java:37)
at com.mangofactory.swagger.models.DefaultModelProvider.dependencies(DefaultModelProvider.java:82)
at com.mangofactory.swagger.readers.ApiModelReader.populateDependencies(ApiModelReader.java:215)
at com.mangofactory.swagger.readers.ApiModelReader.readParametersApiModel(ApiModelReader.java:205)
at com.mangofactory.swagger.readers.ApiModelReader.execute(ApiModelReader.java:82)
at com.mangofactory.swagger.readers.ApiModelReader.execute(ApiModelReader.java:39)
at com.mangofactory.swagger.core.CommandExecutor.execute(CommandExecutor.java:13)
at com.mangofactory.swagger.scanners.ApiListingScanner.scan(ApiListingScanner.java:100)
at com.mangofactory.swagger.core.SwaggerApiResourceListing.initialize(SwaggerApiResourceListing.java:72)
at com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin.initialize(SwaggerSpringMvcPlugin.java:427)
at com.mangofactory.swagger.plugin.SwaggerPluginAdapter.onApplicationEvent(SwaggerPluginAdapter.java:51)
at com.mangofactory.swagger.plugin.SwaggerPluginAdapter.onApplicationEvent(SwaggerPluginAdapter.java:21)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:380)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:334)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:851)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:540)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5027)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
使用 mvn denpendency:tree 调查发现这个依赖
[INFO] +- com.rometools:rome:jar:1.5.0:compile [INFO] | +- com.rometools:rome-utils:jar:1.5.0:compile [INFO] | - org.jdom:jdom:jar:2.0.2:compile
用到的地方是某个Controller ,然后把这个依赖删除,相关的 java 代码删除,就好了。
4. 最简单的api生成,不需要额外配置
<bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />
jar源码访问
访问 /api-docs 这个path即可生成 swagger object
5. 使用继承了 默认配置 自定义配置
@Configuration @EnableWebMvc @EnableSwagger @ComponentScan("com.myapp.controllers") public class CustomJavaPluginConfig { private SpringSwaggerConfig springSwaggerConfig; @Autowired public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) { this.springSwaggerConfig = springSwaggerConfig; } @Bean //Don't forget the @Bean annotation public SwaggerSpringMvcPlugin customImplementation(){ return new SwaggerSpringMvcPlugin(this.springSwaggerConfig) .apiInfo(apiInfo()) .includePatterns(".*pet.*"); } private ApiInfo apiInfo() { ApiInfo apiInfo = new ApiInfo( "My Apps API Title", "My Apps API Description", "My Apps API terms of service", "My Apps API Contact Email", "My Apps API Licence Type", "My Apps API License URL" ); return apiInfo; } }
需要注意的是ComponentScan 不能扫描 org.springframework 否则会报错
Caused by: java.lang.IllegalArgumentException: @EnableAsync annotation metadata was not injected
一般情况下不会扫描这个,因为是在 showcase 的基础上增加了一些代码,就容易出这个问题。
可以直接把这个注解类干掉。
6. 可以把一些配置提出来
7. 把swagger ui 接入
项目代码:git@github.com:witaste/spring-mvc-showcase.git