Spring Boot + Jersey发生FileNotFoundException (No such file or directory)
Posted vincent_ren
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot + Jersey发生FileNotFoundException (No such file or directory)相关的知识,希望对你有一定的参考价值。
我在使用Spring Boot + Jersey 项目,解决了上一篇随笔中的FileNotFoundException,然后又报了一个FileNotFoundException,不过报错信息不一样了
报错信息如下:
ERROR o.a.c.c.C.[Tomcat].[localhost].[/] - StandardWrapper.Throwable org.glassfish.hk2.api.MultiException: A MultiException has 1 exceptions. They are: 1. org.glassfish.jersey.server.internal.scanning.ResourceFinderException: java.io.FileNotFoundException: /home/vincent/myComponent/spring-boot/yishi-service/target/yishi-service-0.5.0.war!/WEB-INF/classes (No such file or directory) at org.jvnet.hk2.internal.Utilities.justCreate(Utilities.java:1007) at org.jvnet.hk2.internal.ServiceLocatorImpl.create(ServiceLocatorImpl.java:962) at org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:1054) at org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:1046) at org.glassfish.jersey.server.ApplicationHandler.createApplication(ApplicationHandler.java:385) at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:342) at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392) at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177) at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369) at javax.servlet.GenericServlet.init(GenericServlet.java:158) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:992) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4913) at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedContext.deferredLoadOnStartup(TomcatEmbeddedContext.java:78) at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.startConnector(TomcatEmbeddedServletContainer.java:256) at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:181) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:297) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:145) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) at com.jiajian.yishi.YishiServiceProjectApplication.main(YishiServiceProjectApplication.java:17) 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.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) at org.springframework.boot.loader.WarLauncher.main(WarLauncher.java:59)
此种问题由于jersey无法通过不解压扫描war包中的包造成的,因为我的ResourceConfig如下:
public class WebApplication extends ResourceConfig { public WebApplication() { System.out.println("WebApplication init...");
//此处造成 packages("com.jiajian.yishi.rest"); property(ServerProperties.TRACING, "ALL"); // Freemarker property(MvcFeature.TEMPLATE_BASE_PATH, "/WEB-INF/fm/"); register(MvcFeature.class); register(FreemarkerMvcFeature.class); // PageViewBehavior register(PageViewContainerRequestFilter.class); register(PageViewContainerResponseFilter.class); } }
目前没有很好的解决方案,
如需使用Jersey可以,通过register(Resource.class) 实现
或者将所以Resource类组成一个单独模块,并通过在启动时unpacked。
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <requiresUnpack> <dependency> <groupId>你的Resource的groupId</groupId> <artifactId>你的Resource的artifactId</artifactId> </dependency> </requiresUnpack> </configuration> </plugin> </plugins> </build>
以上是关于Spring Boot + Jersey发生FileNotFoundException (No such file or directory)的主要内容,如果未能解决你的问题,请参考以下文章
Spring-Boot Jersey:允许 Jersey 提供静态内容
jersey在 spring boot 添加 packages 扫描路径支持