user3629259
user3629259

Reputation: 21

Deploying simple REST example on GlassFish 4.0

I am attempting to take a simple REST example that I found online and deploy to GlassFish 4.0. I am using Eclipse (OEPE distribution) and GlassFish 4.0. When I attempt to "Run As --> Run on Server" the REST example seems to deploy, but I immediately get a HTTP status 404 - Not found error in Eclipse when it attempts to reach out to my context root (http://localhost:8080/RESTFullWS/).

Everything seems to be in place based on what I have been reading, but I still cannot hit the context root nor my service endpoint from a browser. I think I am missing something obvious here, but not sure what is going on.

Service:

package com.example.rest;

@Path("UserInfoService")
public class UserInfo 
{
@GET
    @Path("/name/{i}")
@Produces(MediaType.TEXT_XML)

public String userName(@PathParam("i") String i) 
{
   String name = i;
   return "<User>" + "<Name>" + name + "</Name>" + "</User>";
}

@GET 
@Path("/age/{j}") 
@Produces(MediaType.TEXT_XML)
public String userAge(@PathParam("j") int j) 
{
   int age = j;
   return "<User>" + "<Age>" + age + "</Age>" + "</User>";
}
 }

Web Context defined in glassfish-web.xml:

<glassfish-web-app>
    <context-root>/RESTFullWS</context-root>
</glassfish-web-app>

http://localhost:8080/RESTFullWS/UserInfoService/name/bob  -- gets me the 404.

Is my service even being deployed properly? How can i test?

Upvotes: 1

Views: 8841

Answers (1)

unwichtich
unwichtich

Reputation: 13857

The main problem is that you have chosen a tutorial which is intended for Tomcat, which is just a servlet container and not a full Java EE application server like GlassFish is.

GlassFish 4 comes with Jersey 2.x integrated (this is the JAX-RS implementation which provides the REST stuff). The tutorial suggests to use Jersey 1.x and to package the Jersey libs which is needed if you use Tomcat because Tomcat doesn't come with the required libs (this is also the case for other Java EE technologies like JPA, JSF and more).

But the solution should be easy.

As you posted in the comment you don't have a web.xml, this is ok, it is not required for your case.

You need one class which represents your REST application and loads the endpoint classes. It should look similar to this:

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
public class MyApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(UserInfo.class);
        return classes;
    }
}

As you can see the class UserInfo is added to the list of classes. Add your additional endpoint classes here if you create such.

The annotation javax.ws.rs.core.Application sets the root of your REST endpoints and is added after your main context-root. The URL should look like this:

http://localhost:8080/RESTFullWS/rest/UserInfoService/name/bob

This should be sufficient. Remove all libs from your WEB-INF/lib folder, because GlassFish includes everything you need for your example. If you need a lib for the imports it should be sufficient to depend on the javaee-api package, you don't need to package it with the application. If you use Maven you should have an entry like this in your pom.xml:

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>

See also:

Upvotes: 7

Related Questions