0

I am creating a simple REST service, which owns only REST resource (java class with Jersey annotations). This REST resource needs several Java objects to work. These Java objects are very big, a few hundred MB, so it's slow to load them into RAM. Therefore, I am trying to add these objects as static members of this REST class. However, when I send a request to the REST service, it always throws a NullPonterException. Can anyone explain why these static members are not instantiated before the REST resources(classes) are loaded? Or it's some other reasons?

Thanks you guys in advance.

Note: those static members are just some Maps, and Lists objects, created by a class in a dependency jar.

Edit: assume the InfoConstructor below is one utility class which is responsible for creating those static objects.

code for my REST resource class is as below:

@Path("test")
public class TestResource {
    public static Map<String, Integer> vocMap = InfoConstructor.getVocMap();

    @Get
    @Produce(Media.TEXT_HTML)
    public String testGet() {
         return vocMap.hashCode();
    }
}
Ensom Hodder
  • 1,522
  • 5
  • 18
  • 35
  • 1
    Some code would help. How and when do you instantiate these objects? – toniedzwiedz Aug 10 '12 at 13:49
  • Are those `InfoConstructor` methods also static? Is it possible that `getVocMap` returns `null`? Jersey should have no trouble instantiating your static resources. To verify that you could just set `vocMap` to `new HashMap()` and see if you still get the same NPE when you make the REST call... – condit Aug 10 '12 at 15:26
  • @user463324 Yes, those methods in **InfoConstructor** are static. You're right that Jersey has no difficulties to initialize the static resources. The problem was those static methods cannot correctly find the necessary files to construct a map. So my question becomes how a class in one jar can find some resources (for instance, txt file, in the same jar), when the jar is a dependency for a war. – Ensom Hodder Aug 11 '12 at 09:05
  • Are those resources on your classpath? Have a look at http://stackoverflow.com/questions/1900154/classpath-resource-within-jar – condit Aug 11 '12 at 15:52
  • @user463324 thanks for your reference post. It works when I want to get an InputStream form a txt file inside the jar. However, I cannot manage to create a **File** Object with the obtained URL with the method **getResource**. It throws the "URI is not hierarchical" exception! Could you please give me some hint ? – Ensom Hodder Aug 11 '12 at 21:28
  • @user463324 by the way, the **URI** object is created with "URLObject.toURI()" method. Thanks ! – Ensom Hodder Aug 11 '12 at 21:35
  • Will add that as an answer then... – condit Aug 12 '12 at 01:54

1 Answers1

1

Based on the comments it sounds like the static resources are initialized correctly. More of an issue resolving class path resources. Have a look at Classpath resource within jar

Community
  • 1
  • 1
condit
  • 10,852
  • 2
  • 41
  • 60