Failed to introspect annotated methods on class ..

今天在springboot中使用swagger-UI出现下面的错误,错误日志在最下面给出。程序在软件中并没有报错,为什么一运行就报错呢,缺少类文件,可是明明存在呀。

然后自己看了一下源代码,由于我是把springboot发布到外部tomcat上,所有在springboot启动文件中继承了SpringBootServletInitializer,所有是不是因为SpringBootServletInitializer的原因,导致swagger没有加载呢。

根据这个思路,我把代码调整如下:

 

io.springfox
springfox-swagger-ui
2.4.0



io.springfox
springfox-swagger2
2.4.0

Application.class

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;

@Configuration
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

     @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Swagger.class);
    }
}

Swagger.class

import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger{

public static void main(String[] args) {
SpringApplication.run(Swagger.class, args);
}
@Bean
public Docket testApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("test")
.genericModelSubstitutes(DeferredResult.class)
//.genericModelSubstitutes(ResponseEntity.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(true)
.pathMapping("/test")//api测试请求地址
.select()
.paths(PathSelectors.regex("/common/.*"))//过滤的接口
.build()
.apiInfo(testApiInfo());
}


@Bean
public Docket demoApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("demo")
.genericModelSubstitutes(DeferredResult.class)
//  .genericModelSubstitutes(ResponseEntity.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(false)
.pathMapping("/")
.select()
.paths(PathSelectors.regex("/comm.*"))//过滤的接口
.build()
.apiInfo(demoApiInfo());
}

private ApiInfo testApiInfo() {
Contact contact = new Contact("王念", "http://my.oschina.net/wangnian", "[email protected]");
ApiInfo apiInfo = new ApiInfo("某API接口",//大标题
"REST风格API",//小标题
"0.1",//版本
"www.baidu.com",
contact,//作者
"主页",//链接显示文字
""//网站链接
);
return apiInfo;
}

private ApiInfo demoApiInfo() {
Contact contact = new Contact("王念", "http://my.oschina.net/wangnian", "[email protected]");
ApiInfo apiInfo = new ApiInfo("某API接口",//大标题
"REST风格API",//小标题
"0.1",//版本
"www.baidu.com",
contact,//作者
"主页",//链接显示文字
""//网站链接
);
return apiInfo;
}
}

然后。。还是报错。后来查了一下,有的说需要在pom文件中添加下面代码:

 
org.apache.tomcat.embed
tomcat-embed-jasper

结果还是报错

后经过多次派出,发现是类引入出现问题,去掉下面引入进正确了:



xml-apis
xml-apis
1.0.b2

下面给出报错日志:

  .   ____  ___ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::(v1.3.1.RELEASE)

2016-09-23 09:16:27.134  INFO 4788 --- [ost-startStop-1] com.ilex.jiutou.Application  : Starting Application on XHJ4XGJHX0LDDYM with PID 4788 (Z:\zhaolixiang\java\javaweb\software\apache-tomcat-7.0.69\webapps\www.jiutouxiang.com\WEB-INF\classes started by Administrator in Z:\zhaolixiang\java\javaweb\software\apache-tomcat-7.0.69\bin)
2016-09-23 09:16:27.141  INFO 4788 --- [ost-startStop-1] com.ilex.jiutou.Application  : No active profile set, falling back to default profiles: default
2016-09-23 09:16:27.520  INFO 4788 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@bf16099: startup date [Fri Sep 23 09:16:27 CST 2016]; root of context hierarchy
2016-09-23 09:16:28.272  WARN 4788 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.ilex.jiutou.Application]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class com.ilex.jiutou.Application
2016-09-23 09:16:28.283 ERROR 4788 --- [ost-startStop-1] o.s.boot.SpringApplication   : Application startup failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.ilex.jiutou.Application]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class com.ilex.jiutou.Application
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:181) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:321) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.boot.context.web.SpringBootServletInitializer.run(SpringBootServletInitializer.java:149) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:129) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:85) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175) [spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5573) [catalina.jar:7.0.69]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) [catalina.jar:7.0.69]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899) [catalina.jar:7.0.69]
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875) [catalina.jar:7.0.69]
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) [catalina.jar:7.0.69]
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1260) [catalina.jar:7.0.69]
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:2002) [catalina.jar:7.0.69]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_20]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_20]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_20]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_20]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_20]
Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class com.ilex.jiutou.Application
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:163) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:291) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:231) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    ... 26 common frames omitted
Caused by: java.lang.NoClassDefFoundError: springfox/documentation/spring/web/plugins/Docket
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_20]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2688) ~[na:1.8.0_20]
    at java.lang.Class.getDeclaredMethods(Class.java:1962) ~[na:1.8.0_20]
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:152) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    ... 30 common frames omitted
Caused by: java.lang.ClassNotFoundException: springfox.documentation.spring.web.plugins.Docket
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1858) ~[catalina.jar:7.0.69]
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1701) ~[catalina.jar:7.0.69]
    ... 34 common frames omitted

你可能感兴趣的:(Failed to introspect annotated methods on class ..)