javaweb-servicesrestservletsgrizzly

Use Grizzly as a web service engine


I'm try to create a Grizzly web service engine but some elements are missing.

Here is what I would like to do:

What I already know :

What I would like to know :

Here is what I have done with the classloader

File file  = new File("c:\\Users\\User\\Desktop\\myresource.jar");
 URL url = file.toURI().toURL();  
 URL[] urls = new URL[]{url};
 ClassLoader cl = new URLClassLoader(urls);

URLClassLoader child = new URLClassLoader (urls, this.getClass().getClassLoader());

Class<?> classToLoad = Class.forName ("Test.ExternalWS.MyResource", true, child);
Method method = classToLoad.getDeclaredMethod ("getIt");
Object instance = classToLoad.newInstance ();
Object result = method.invoke (instance);
this.WSInstance = classToLoad.newInstance();

Nothing happens when I try to reach localhost:8080/....../myresource.

I tried all possible urls and put a breakpoint... never reached.


Solution

  • Not sure which Jersey version you're using. In Jersey 2 I made it work this way:

        File file = new File("/path/resource1.jar");
        URL url = file.toURI().toURL();
        URL[] urls = new URL[]{url};
        ClassLoader cl = new URLClassLoader(urls, Main.class.getClassLoader());
    
        ResourceConfig rc = new ResourceConfig()
                .setClassLoader(cl)
                .files("/path/resource1.jar");
        HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(
                BASE_URI, rc);
    

    May be there is more elegant way...