java - 在 Jersey 项目中找不到文件

标签 java tomcat jersey-2.0

我正在尝试从 jersey webapp 的资源类中的文件加载 Properties。这是资源,我听从了建议 here

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("persons")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class PersonResource {

public PersonResource() 
{
     Properties props = new Properties();
     InputStream is = PersonResource.class.getClass().getClassLoader().getResourceAsStream("WEB-INF/test.txt");
     if(is == null) { System.out.println("Inputstream is null");}
     else 
     {
         try 
         { 
             props.load(is);
             System.out.println(props.propertyNames());
         } 
         catch (IOException e) { e.printStackTrace(); }
     }

}

@GET
public List<Person> getAllPersons()
{
    List<Person> res = new ArrayList<>();
    res.add(new Person("peter", "sullivan"));
    res.add(new Person("claire", "wood"));
    return res;
}

@GET
@Path("/{fn}")
public Person getPerson(@PathParam("fn") String fn)
{
    return new Person("peter", "sullivan");
}

这是我的文件夹结构

folder structure

test.txt 位于 WebContent/WEB-INF/ 中。该文件只是一行“key=value”。我将 WebContent 文件夹添加为源文件夹。

当我在 tomcat 7 上运行该应用程序时,我得到

java.lang.NullPointerException
demo.rest.jersey.resource.PersonResource.<init>(PersonResource.java:25)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:423)
org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionHelper.java:1375)
org.jvnet.hk2.internal.ClazzCreator.createMe(ClazzCreator.java:272)
org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:366)
org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)
org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:162)
org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2022)

出现错误的第25行是InputStream is = PersonResource.class.getClass().getClassLoader().getResourceAsStream("WebContent/WEB-INF/test.txt");

感谢您的帮助!

最佳答案

如果您使用的是 eclipse,请右键单击 test.txt 文件,然后按属性。

它打开一个对话框,在那里你会找到那个文件的路径。复制该路径并运行它。

关于java - 在 Jersey 项目中找不到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47655444/

相关文章:

java - 将 Datastax Cassandra 结果集与 Java 8 并行流一起使用 - 快速

java - 如何在没有java的情况下运行SQL命令?

java - Netty导致tomcat内存泄漏

spring - 使用集成的 Tomcat 部署 Maven 项目

java - 如何使用 Java 应用程序从网站提取 CSV 文件?

java - Tomcat 上的 Tapestry Web App 偶尔会吐出垃圾

java - NoSuchFieldError : INCLUDE_ALL 错误

java - 在 Osgi 中以编程方式注册 ServletContainer

java - 使用 JSON 对象数组执行 Post 时出现 MessageBodyProviderNotFoundException

java - 从 System.in 读入 - Java