Springboot the pits encountered during the integration of swagger

Source: Internet
Author: User
Tags constructor documentation error handling
Springboot steps to integrate swagger and solve the problem first step: Introducing Swagger Dependent files: <dependency> <groupid>io.springfox</groupid&gt          ; <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency&gt ; <dependency> <groupId>io.springfox</groupId> <artifactid>springfox-swagger -ui</artifactid> <version>2.7.0</version> </dependency> The version here is 2.7.0. 
The second step: write the configuration file, pure Java configuration. 
@Configuration 
@EnableSwagger2 
public class Swagger2config { 
@Bean 
Public Docket Createrestapi () { 
return new Docket (documentationtype.swagger_2) 
. Apiinfo (Apiinfo ()) 
. Select () 
. APIs (Requesthandlerselectors.basepackage ("Com.xxx.demo.web"))//scan the package name of the controller that you are writing. 
. Paths (Pathselectors.any ()) 
. build (); 

Private Apiinfo Apiinfo () { 
return new Apiinfobuilder () 
. Title ("Springboot using swagger to build API documentation")//swagger title 
. Description ("Simple and elegant Restfun style")//define Yourself 
. Termsofserviceurl ("http://blog.csdn.net/")//self-defined 
. Version ("1.0") 
. build (); 


In this case, when you start the Springboot startup class, and then according to the original port (if you configure the access in the configuration file must add the project name, which own remember.) For example, I was visiting: http://localhost:8080/swagger-ui.html. This allows you to see the interfaces you have defined. But if you want to write the test class to test their own written interface, the swagger configured in this way, when the unit test will be reported error. As follows:
org.springframework.transaction.annotation.proxytransactionmanagementconfiguration$ $EnhancerBySpringCGLIB $$ CFBCB6CB] is not eligible for getting processed by all beanpostprocessors (for example:not eligible for auto-proxying)
2017-05-16 11:58:07.671 WARN 11268---[main] s.c.a.annotationconfigapplicationcontext:exception encountered during CO ntext initialization-cancelling Refresh Attempt:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating Bean with Name ' documentationpluginsbootstrapper ' defined in URL [jar:file:/e:/jar/io/springfox/ springfox-spring-web/2.2.2/springfox-spring-web-2.2.2.jar!/springfox/documentation/spring/web/plugins/ Documentationpluginsbootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 1; Nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating beans with Name ' Webmvcrequesthandlerprovider ' defined in URL [jar:file:/e:/jar/io/springfox/springfox-spring-web/2.2.2/ Springfox-spring-web-2.2.2.jar!/springfox/documentation/spring/web/plugins/webmvcrequesthandlerprovider.class] : Unsatisfied dependency expressed through constructor parameter 0; Nested exception is Org.springframework.beaNs.factory.NoSuchBeanDefinitionException:No qualifying Bean of type ' java.util.list< Org.springframework.web.servlet.mvc.method.requestmappinginfohandlermapping> ' available:expected at least 1 bean Which qualifies as Autowire candidate. Dependency annotations: {}
2017-05-16 11:58:07.675 WARN 11268---[main] o.s.boot.springapplication:error handling failed (Error creating bean wit H name ' Delegatingapplicationlistener ' defined in class path resource [org/springframework/security/config/annotation/ Web/configuration/websecurityconfiguration.class]: Beanpostprocessor before instantiation of Bean failed; Nested exception is org.springframework.beans.factory.BeanCreationException:Error creating beans with Name ' Org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration ': Initialization of beans failed ; Nested exception is Org.springframework.beans.factory.NoSuchBeanDefinitionException:No bean named ' Org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry ' available)
2017-05-16 11:58:07.745 ERROR 11268---[main] o.s.boot.springapplication:application startup failed

Org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with Name ' Documentationpluginsbootstrapper ' defined in URL [jar:file:/e:/jar/io/springfox/springfox-spring-web/2.2.2/ springfox-spring-web-2.2.2.jar!/springfox/documentation/spring/web/plugins/ Documentationpluginsbootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 1; Nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating beans with Name ' Webmvcrequesthandlerprovider ' defined in URL [jar:file:/e:/jar/io/springfox/springfox-spring-web/2.2.2/ Springfox-spring-web-2.2.2.jar!/springfox/documentation/spring/web/plugins/webmvcrequesthandlerprovider.class] : Unsatisfied dependency expressed through constructor parameter 0; Nested exception is Org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type ' Java.util.list<org.springframework.web.servlet.mvc.method.requestmappinginfohandlermapping> ' Available:expected at least 1 bean which qualifies as Autowire candidate. Dependency annotations: {}
At Org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray (Constructorresolver.java : 749) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor (Constructorresolver.java : 189) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor ( abstractautowirecapablebeanfactory.java:1193) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance ( abstractautowirecapablebeanfactory.java:1095) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean ( abstractautowirecapablebeanfactory.java:513) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean ( abstractautowirecapablebeanfactory.java:483) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.abstractbeanfactory$1.getobject (abstractbeanfactory.java:306) ~[ Spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton ( defaultsingletonbeanregistry.java:230) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (abstractbeanfactory.java:302) ~[ Spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.AbstractBeanFactory.getBean (abstractbeanfactory.java:197) ~[ Spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons ( defaultlistablebeanfactory.java:761) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization ( abstractapplicationcontext.java:866) ~[spring-context-4.3.8.release.jar:4.3.8.release]
At Org.springframework.context.support.AbstractApplicationContext.refresh (abstractapplicationcontext.java:542) ~ [Spring-context-4.3.8.release.jar:4.3.8.release]
At Org.springframework.boot.SpringApplication.refresh (springapplication.java:737) ~[ Spring-boot-1.5.3.release.jar:1.5.3.release]
At Org.springframework.boot.SpringApplication.refreshContext (springapplication.java:370) ~[ Spring-boot-1.5.3.release.jar:1.5.3.release]
At Org.springframework.boot.SpringApplication.run (springapplication.java:314) ~[spring-boot-1.5.3.release.jar : 1.5.3.RELEASE]
At Org.springframework.boot.test.SpringApplicationContextLoader.loadContext (Springapplicationcontextloader.java : 103) [Spring-boot-test-1.4.2.release.jar:1.4.2.release]
At Org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal ( DEFAULTCACHEAWARECONTEXTLOADERDELEGATE.JAVA:98) [Spring-test-4.3.8.release.jar:4.3.8.release]
At Org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext ( defaultcacheawarecontextloaderdelegate.java:116) [Spring-test-4.3.8.release.jar:4.3.8.release]
At Org.springframework.test.context.support.DefaultTestContext.getApplicationContext (defaulttestcontext.java:83 ) [Spring-test-4.3.8.release.jar:4.3.8.release]
At Org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies ( dependencyinjectiontestexecutionlistener.java:117) [Spring-test-4.3.8.release.jar:4.3.8.release]
At Org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance ( DEPENDENCYINJECTIONTESTEXECUTIONLISTENER.JAVA:83) [Spring-test-4.3.8.release.jar:4.3.8.release]
At Org.springframework.test.context.TestContextManager.prepareTestInstance (testcontextmanager.java:230) [ Spring-test-4.3.8.release.jar:4.3.8.release]
At Org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest (springjunit4classrunner.java:228) [Spring-test-4.3.8.release.jar:4.3.8.release]
At Org.springframework.test.context.junit4.springjunit4classrunner$1.runreflectivecall ( springjunit4classrunner.java:287) [Spring-test-4.3.8.release.jar:4.3.8.release]
At Org.junit.internal.runners.model.ReflectiveCallable.run (reflectivecallable.java:12) [junit-4.12.jar:4.12]
At Org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock (springjunit4classrunner.java:289 ) [Spring-test-4.3.8.release.jar:4.3.8.release]
Org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate ( RUNBEFORETESTCLASSCALLBACKS.JAVA:61) [Spring-test-4.3.8.release.jar:4.3.8.release]
At Org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate ( RUNAFTERTESTCLASSCALLBACKS.JAVA:70) [Spring-test-4.3.8.release.jar:4.3.8.release]
At Org.junit.runners.ParentRunner.run (parentrunner.java:363) [junit-4.12.jar:4.12]
At Org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run (springjunit4classrunner.java:191) [ Spring-test-4.3.8.release.jar:4.3.8.release]
At Org.junit.runner.JUnitCore.run (junitcore.java:137) [junit-4.12.jar:4.12]
At Com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs (junit4ideatestrunner.java:68) [Junit-rt.jar:na]
At Com.intellij.rt.execution.junit.ideatestrunner$repeater.startrunnerwithargs (IdeaTestRunner.java:51) [ Junit-rt.jar:na]
At Com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart (junitstarter.java:237) [Junit-rt.jar:na]
At Com.intellij.rt.execution.junit.JUnitStarter.main (junitstarter.java:70) [Junit-rt.jar:na]
At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) ~[na:1.7.0_79]
At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:57) ~[na:1.7.0_79]
At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43) ~[na:1.7.0_79]
At Java.lang.reflect.Method.invoke (method.java:606) ~[na:1.7.0_79]
At Com.intellij.rt.execution.application.AppMain.main (appmain.java:147) [Idea_rt.jar:na]
caused by:org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with Name ' Webmvcrequesthandlerprovider ' defined in URL [jar:file:/e:/jar/io/springfox/springfox-spring-web/2.2.2/ Springfox-spring-web-2.2.2.jar!/springfox/documentation/spring/web/plugins/webmvcrequesthandlerprovider.class] : Unsatisfied dependency expressed through constructor parameter 0; Nested exception is Org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type ' Java.util.list<org.springframework.web.servlet.mvc.method.requestmappinginfohandlermapping> ' available: Expected at least 1 bean which qualifies as Autowire candidate. Dependency annotations: {}
At Org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray (Constructorresolver.java : 749) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor (Constructorresolver.java : 189) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor ( abstractautowirecapablebeanfactory.java:1193) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance ( abstractautowirecapablebeanfactory.java:1095) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean ( abstractautowirecapablebeanfactory.java:513) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean ( abstractautowirecapablebeanfactory.java:483) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.abstractbeanfactory$1.getobject (abstractbeanfactory.java:306) ~[ Spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton ( defaultsingletonbeanregistry.java:230) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (abstractbeanfactory.java:302) ~[ Spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.AbstractBeanFactory.getBean (abstractbeanfactory.java:202) ~[ Spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate (dependencydescriptor.java:208 ) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency ( defaultlistablebeanfactory.java:1138) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency ( defaultlistablebeanfactory.java:1066) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument ( constructorresolver.java:835) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
At Org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray (Constructorresolver.java : 741) ~[spring-beans-4.3.8.release.jar:4.3.8.release]
... Common frames omitted
caused By:org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type ' java.util.List <org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping> ' available:expected at least 1 Bean which qualifies as Autowire candidate. Dependency annotations: {}



at this point we simply add @springbootapplication and @WebAppConfiguration two annotations to our test class, which solves the problem.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.