springmvc3.1.2集成swagger失败,升级到3.2.9后集成成功

背景

公司要上自动化测试,该工具可以通过swagger快速导入接口。本人手头上负责几个springmvc版本为3.1.2的服务,需要集成swagger。

 

集成过程

1. 导入pom.xml (此时spring的版本号是3.1.2.RELEASE)

  

     com.fasterxml.jackson.core
     jackson-databind
     2.8.9
   
2.6.1	


      io.springfox
      springfox-swagger2
      ${swagger.version}



      io.springfox
      springfox-swagger-ui
      ${swagger.version}
 

2. 配置类SwaggerConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = {"com.*.*.controller"})  
public class SwaggerConfig{

	@Bean
	public Docket api() {
		return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()).build()
				.apiInfo(apiInfo());
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("开放接口API").description("HTTP对外开放接口").version("1.0.0")
				.termsOfServiceUrl("http://xxx.xxx.com").license("LICENSE").licenseUrl("http://xxx.xxx.com").build();
	}

}

3.启动tomcat报错

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined in URL [jar:file:/D:/code/.metadata/.plugins/org.eclipse.wst.server.core/tmp5/wtpwebapps/wjw_02/WEB-INF/lib/springfox-spring-web-2.6.1.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor argument with index 5 of type [springfox.documentation.spi.service.contexts.Defaults]: : Error creating bean with name 'defaults' defined in class path resource [springfox/documentation/spring/web/SpringfoxWebMvcConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public springfox.documentation.spi.service.contexts.Defaults springfox.documentation.spring.web.SpringfoxWebMvcConfiguration.defaults()] threw exception; nested exception is java.lang.NoSuchFieldError: PATCH; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaults' defined in class path resource [springfox/documentation/spring/web/SpringfoxWebMvcConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public springfox.documentation.spi.service.contexts.Defaults springfox.documentation.spring.web.SpringfoxWebMvcConfiguration.defaults()] threw exception; nested exception is java.lang.NoSuchFieldError: PATCH
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:730)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:196)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1035)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:939)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5110)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5633)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1694)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1684)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaults' defined in class path resource [springfox/documentation/spring/web/SpringfoxWebMvcConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public springfox.documentation.spi.service.contexts.Defaults springfox.documentation.spring.web.SpringfoxWebMvcConfiguration.defaults()] threw exception; nested exception is java.lang.NoSuchFieldError: PATCH
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:581)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1015)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:876)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:795)
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723)
	... 24 more
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public springfox.documentation.spi.service.contexts.Defaults springfox.documentation.spring.web.SpringfoxWebMvcConfiguration.defaults()] threw exception; nested exception is java.lang.NoSuchFieldError: PATCH
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:169)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:570)
	... 37 more
Caused by: java.lang.NoSuchFieldError: PATCH
	at springfox.documentation.spi.service.contexts.Defaults.initResponseMessages(Defaults.java:270)
	at springfox.documentation.spi.service.contexts.Defaults.init(Defaults.java:155)
	at springfox.documentation.spi.service.contexts.Defaults.(Defaults.java:72)
	at springfox.documentation.spring.web.SpringfoxWebMvcConfiguration.defaults(SpringfoxWebMvcConfiguration.java:68)
	at springfox.documentation.spring.web.SpringfoxWebMvcConfiguration$$EnhancerByCGLIB$$72ed77a0.CGLIB$defaults$0()
	at springfox.documentation.spring.web.SpringfoxWebMvcConfiguration$$EnhancerByCGLIB$$72ed77a0$$FastClassByCGLIB$$3c92cb46.invoke()
	at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:280)
	at springfox.documentation.spring.web.SpringfoxWebMvcConfiguration$$EnhancerByCGLIB$$72ed77a0.defaults()
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:149)
	... 38 more

4.升级spring版本为3.2.9.RELEASE

5.再次启动tomcat正常

6.访问http://localhost:8080/wjw_02/swagger-ui.html 异常

springmvc3.1.2集成swagger失败,升级到3.2.9后集成成功_第1张图片

7.打开F12发现404

springmvc3.1.2集成swagger失败,升级到3.2.9后集成成功_第2张图片

8. 查看web.xml,发现是自己的个人配置问题

springmvc3.1.2集成swagger失败,升级到3.2.9后集成成功_第3张图片

 

9.加上url-pattern

 (备注,因为个别原因,不能直接从/ext/*   修改为/* , 所以采用新增url-pattern)


   springServlet
   /*

springmvc3.1.2集成swagger失败,升级到3.2.9后集成成功_第4张图片

10.启动tomcat后打开链接正常

springmvc3.1.2集成swagger失败,升级到3.2.9后集成成功_第5张图片

11.打war包后放linux环境启动异常

2019-06-21 11:08:15,555 [localhost-startStop-2] ERROR [org.springframework.web.context.ContextLoader] - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: com.google.common.collect.FluentIterable.toList()Lcom/google/common/collect/ImmutableList;
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:633)
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
 at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410)
 at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
 at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4753)
 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5215)
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
 at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
 at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
 at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:986)
 at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)
 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
 at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodError: com.google.common.collect.FluentIterable.toList()Lcom/google/common/collect/ImmutableList;
 at springfox.documentation.spring.web.ObjectMapperConfigurer.jackson2Converters(ObjectMapperConfigurer.java:78)
 at springfox.documentation.spring.web.ObjectMapperConfigurer.configureMessageConverters(ObjectMapperConfigurer.java:63)
 at springfox.documentation.spring.web.ObjectMapperConfigurer.postProcessBeforeInitialization(ObjectMapperConfigurer.java:47)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:396)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1505)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
 ... 24 more

12.上网查询是guava包的问题

项目里有自己引用的guava版本是13.0
而springfox-swagger2里是使用18.0的版本,把自己的guava注释掉

13.运行linux环境的tomcat正常

springmvc3.1.2集成swagger失败,升级到3.2.9后集成成功_第6张图片

你可能感兴趣的:(swagger)