Question

I am trying to run a simple "Hello World" RESTful web service on my machine. I use Eclipse Kepler and GlassFish 4.0. I am able to deploy the service and I see it on the admin pages of GlassFish but when I try to access to it I get the following error: "HTTP Status 404 - Not Found".

Herein the code of the simple service:

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

@Path("hello")
public class HelloRest {
    @SuppressWarnings("unused")
    @Context
    private UriInfo context;

    /**
     * Default constructor. 
     */
    public HelloRest() {
        // TODO Auto-generated constructor stub
    }

    /**
     * Retrieves representation of an instance of HelloRest
     * @return an instance of String
     */
    @GET
    @Produces("application/xml")
    public String getXml() {
        // TODO return proper representation object
        return "<greeting>Hello REST</greeting>";
    }

    /**
     * PUT method for updating or creating an instance of HelloRest
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/xml")
    public void putXml(String content) {
    }

}

In order to access to the service I try the following URL: http://127.0.0.1:8080/hello-rest/hello, where hello-rest is the name of the Eclipse project and the root path suggested by the admin page of GlassFish.

Was it helpful?

Solution

Your code seems to be ok, so the problem most likely is that you have not defined the base url for your service. You need to tell JAX-RS (Jersey is the implementation in GlassFish) which url pattern it must intercept as your endpoint (base url).

One way of achieving this is using an Application Class which can be added to any package in the project (you could alternatively define the necessary in a web.xml file which I won't cover). The following is the code:

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("your-base-url")
public class ApplicationConfig extends Application {

}

You use the @ApplicationPath annotation to define your base url. You can then access your service at http://127.0.0.1:8080/hello-rest/your-base-url/hello

OTHER TIPS

@GET
@Produces("application/xml")
@path("/getxml")
public String getXml() {
    // TODO return proper representation object
    return "<greeting>Hello REST</greeting>";
}

Your url should be http://localhost:8080/hello-rest/hello/getxml

You should mention the path at the method level , so the request is redirected to appropriate method.

  • You can use SOAPUI to test the service. Please make sure you have choose the correct method.

  • Method as 'GET'

  • End point as 'hostname:8080'

  • Resource as 'hello-rest/hello/getxml'

    If you face the same issue. Please attach more logs / exception in tomcat console.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top