【问题标题】:java - cannot find file in jersey projectjava - 在泽西项目中找不到文件
【发布时间】:2017-12-05 14:00:51
【问题描述】:

我正在尝试从 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");
}

}

这是我的文件夹结构

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");

感谢您的帮助!

【问题讨论】:

    标签: java tomcat jersey-2.0


    【解决方案1】:

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

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

    【讨论】:

    • 不,即使tomcat在本地主机上运行也不行
    【解决方案2】:

    问题在于路径本身。它应该是相对路径。 就像您必须从当前文件夹中出来并转到我们拥有 test.txt 的文件夹。

    或

    将 test.txt 复制到您拥有 PersonResource .java 的文件夹中,路径应为“test.txt”

    【讨论】:

    • 你说对了一部分。该文件必须位于源文件夹中(例如 webapp/WEB-INF/test.txt,其中“webapp”是源文件夹),然后您可以使用“WEB-INF/test.txt”引用它。但是另一个重要的一点是,您只能通过 ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource(path).getFile()); 从字符串路径初始化 File 对象
    【解决方案3】:

    答案是here。现在可以了:

    @Path("persons")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public class PersonResource implements ServletContextListener
    {
    
    @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");
    }
    
    @Override
    public void contextDestroyed(ServletContextEvent ctxt) {
        System.out.println("Destroying context");
    }
    
    @Override
    public void contextInitialized(ServletContextEvent ctxt) {
        System.out.println("PersonResource initialized!");
    
        Properties props = new Properties();
        try(InputStream is = ctxt.getServletContext().getResourceAsStream("/WEB-INF/test.txt"))
        {
            props.load(is);
            System.out.println("key= " + props.getProperty("key"));
        }
        catch(Exception e)
        {
            System.out.println("Cannot load test.txt");
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-16
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 2014-12-05
      • 1970-01-01
      • 2016-03-10
      相关资源
      最近更新 更多