0

I am trying to build a very basic Restful server in Eclipse using jersey and GrizzlyHttpServerFactory. Below is the code:

The server

package com.example.rest;
class WebServr{

    public static final String BASE_URI = "http://localhost:8080/myapp/";

    public static HttpServer startServer() {
        // create a resource config that scans for JAX-RS resources and providers
        // in com.example.rest package
        final ResourceConfig rc = new ResourceConfig().packages("com.example.rest");

        // create and start a new instance of grizzly http server
        // exposing the Jersey application at BASE_URI
        return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
    }

    public static void main( String[] args ) {
        WebServr wb = new WebServr();
        wb.startServer();
    }
}

The rest component

package com.example.rest;
@Path("/amp")
@Produces("text/html")
public class Endpoint{
    @GET
    @Path("/test")
    public Response test() {
        return Response.ok().entity("SUCCESS").build();
    }
}

But when I create a simple “jar” file by exporting my compiled java files using above code, I can see my server is running(can see output in netstat –an | grep port) but my restful client is returned with “404 error”(though I can see a new http connection is being established at server side). But when I create a jar file using eclipse option of “Runnable jar”, it works perfectly fine(my client is served properly). Can anyone give any suggestion why it is happening.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
pjain
  • 1,149
  • 2
  • 14
  • 28
  • Which URL do you test? Try this http://localhost:8080/myapp/amp/test – Nicolas Filotto Nov 23 '16 at 16:10
  • In both the cases I try same URL. I actually deploy it to a linux machine and from "postman" I access my server URL. I felt somewhere my issue is because of the given package name(com.example.rest) in createHttpServer. My main issue is why same code base with two different modes of creating ''jar" file in eclipse is behaving so differently in this case. – pjain Nov 23 '16 at 16:16
  • see this question/answer http://stackoverflow.com/questions/31992461/how-to-run-jersey-server-webservice-server-without-using-tomcat – borowis Nov 28 '16 at 16:48
  • I can't use maven.If I create jar using maven that runs fine. My issue is why it is not running when we are simply creating a jar from eclipse and wanted to create a RestFul service, its not working. – pjain Nov 29 '16 at 06:55

1 Answers1

0

i am facing the same issue,

The short answer : place your compiled classes in web-inf/classes

The long version :) I am convinced that because of ClassLoader in tomcat, web-inf/classes is being loaded before web-inf/lib, when you place your classes in web-inf/classes it's being loaded by classloader, then when web-inf/lib jars are loaded which jersey one of them, jersey will scan for the declared classes, and will find them (WIN)

BUT

when classes are in a jar file (in web-inf/lib) they will be loaded yes, but MOSTLY they are loaded after jersey, and as a result when jersey scans for the endpoints it will find nothing.

i know this question is a bit old, but this could help someone facing same issue.

For a solution, actually i am still looking, if i can control the order of jars loading, or i may implement some classes (Application, Configuration) from jersey and figure out a way to let jersey find them.

Yazan
  • 6,074
  • 1
  • 19
  • 33