I am trying to load Properties
from a file in a resource class of a jersey webapp. This is the resource and I followed the advice 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");
}
}
This is my folder structure
where the test.txt
is located in WebContent/WEB-INF/
. The file is just a single line "key=value". I added the WebContent
folder as a source folder.
When I run the app on tomcat 7, I get
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)
Line 25 where the error occurs is InputStream is = PersonResource.class.getClass().getClassLoader().getResourceAsStream("WebContent/WEB-INF/test.txt");
Thanks for the help!