美文网首页
Dropwizard整合Swagger打包的jar文件运行报错-

Dropwizard整合Swagger打包的jar文件运行报错-

作者: Wpixel | 来源:发表于2018-11-20 18:57 被阅读0次

子夏曰:“贤贤易色;事父母,能竭其力;事君,能致其身;与朋友交,言而有信。虽曰未学,吾必谓之学矣。”

用Dropwizard开发微服务为了更好的编写api文档,就整合了Swagger,结果搞出一大推Bug。

(Ps. 百度是真的渣,查了一下午都是一些没用的资料,Google几分钟就查到了)

项目打包成jar文件运行结果爆出一大堆的错误,虽然结果能够访问,但是错误是不被允许的

太多了就截取最后一点
WARN   [2018-11-20 18:19:54.269] [main] org.reflections.Reflections - could not get type for name javax.servlet.jsp.JspApplicationContext from any class loader
org.reflections.ReflectionsException: could not get type for name javax.servlet.jsp.JspApplicationContext
        at org.reflections.ReflectionUtils.forName(ReflectionUtils.java:390)
        at org.reflections.Reflections.expandSuperTypes(Reflections.java:381)
        at org.reflections.Reflections.<init>(Reflections.java:126)
        at io.swagger.jaxrs.config.BeanConfig.classes(BeanConfig.java:276)
        at io.swagger.jaxrs.config.BeanConfig.scanAndRead(BeanConfig.java:240)
        at io.swagger.jaxrs.config.BeanConfig.setScan(BeanConfig.java:221)
        at io.federecio.dropwizard.swagger.SwaggerBundleConfiguration.build(SwaggerBundleConfiguration.java:318)
        at io.federecio.dropwizard.swagger.SwaggerBundle.run(SwaggerBundle.java:80)
        at com.mango.bundles.SwitchableSwaggerBundle.run(SwitchableSwaggerBundle.java:18)
        at com.mango.bundles.SwitchableSwaggerBundle.run(SwitchableSwaggerBundle.java:1)
        at io.federecio.dropwizard.swagger.SwaggerBundle.run(SwaggerBundle.java:46)
        at io.dropwizard.setup.Bootstrap.run(Bootstrap.java:200)
        at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:42)
        at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:87)
        at io.dropwizard.cli.Cli.run(Cli.java:78)
        at io.dropwizard.Application.run(Application.java:93)
        at com.mango.MangoApplication.main(MangoApplication.java:19)
Caused by: java.lang.ClassNotFoundException: javax.servlet.jsp.JspApplicationContext
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at org.reflections.ReflectionUtils.forName(ReflectionUtils.java:388)
        ... 16 common frames omitted

错误原因:org.reflections的包版本默认依赖下载的是0.9.11版,降级至0.9.10有效。

既然找到原因了,就到pom文件里改吧

1. 排除掉org.reflections文件,默认加载的reflections包不加载进来,自己来手动添加

<dependency>
    <groupId>com.smoketurner</groupId>
    <artifactId>dropwizard-swagger</artifactId>
    <version>1.3.7-1</version>
    <!-- 添加下面的代码 -->
    <exclusions>
      <exclusion>
        <groupId>org.reflections</groupId>
        <artifactId>reflections</artifactId>
      </exclusion>
    </exclusions>
  </dependency>

2. 添加reflections 0.9.10的包

<dependency>
  <groupId>org.reflections</groupId>
  <artifactId>reflections</artifactId>
  <version>0.9.10</version>
</dependency>

ok 打包

[INFO] Including javax.activation:javax.activation-api:jar:1.2.0 in the shaded jar.
[INFO] Including org.reflections:reflections:jar:0.9.10 in the shaded jar.
[INFO] Including com.google.guava:guava:jar:15.0 in the shaded jar.
[INFO] Including org.javassist:javassist:jar:3.19.0-GA in the shaded jar.
[INFO] Including com.google.code.findbugs:annotations:jar:2.0.1 in the shaded jar.
[INFO] Including com.google.dagger:dagger:jar:2.19 in the shaded jar.
[INFO] Including javax.inject:javax.inject:jar:1 in the shaded jar.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.865 s
[INFO] Finished at: 2018-11-20T18:43:16+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.4.1:shade (default) on project Mango-WJM-API: Error creating shaded jar: invalid LOC header (bad signature) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

WTF Why 怎么又错了,,我运行了一遍也报错了,,肯定又是包冲突了

再见,我Google去了,如果我活着回来就继续写。。。。。

我找到原因了,原来reflections 0.9.10 默认依赖的包有两个要去掉

<dependency>
  <groupId>org.reflections</groupId>
  <artifactId>reflections</artifactId>
  <version>0.9.10</version>
  <!-- 去掉这两个包-->
  <exclusions>
    <exclusion>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
    </exclusion>
    <exclusion>
      <groupId>com.google.code.findbugs</groupId>
      <artifactId>annotations</artifactId>
    </exclusion>
   </exclusions>
</dependency>

编译打包 运行一气呵成


作者是一名自由程序员,住在上海,喜欢音乐、小说、旅行、以及编程。

P.S. 如果您喜欢这篇文章并且希望学习编程技术的话,请关注一下

相关文章

网友评论

      本文标题:Dropwizard整合Swagger打包的jar文件运行报错-

      本文链接:https://www.haomeiwen.com/subject/jtgxqqtx.html