focode
focode

Reputation: 716

restful webservice with glassfish server

I have created the restful web service with netbeans and glassfish, but when I am trying to hit the rest service my browser shows 404 not found exception.

here is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.pps.rest.service</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>com.pps.servlet.TestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/TestServlet</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

and here is the rest service:

@Path("/todo")
public class TodoResource {

    // This method is called if XMLis request
    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Todo getXML() {
        Todo todo = new Todo();
        todo.setSummary("This is my first todo");
        todo.setDescription("This is my first todo");
        return todo;
    }

    // This can be used to test the integration with the browser
    @Path("/todo1")
    @GET
    @Produces({MediaType.TEXT_XML})
    public Todo getHTML() {
        Todo todo = new Todo();
        todo.setSummary("This is my first todo");
        todo.setDescription("This is my first todo");
        return todo;
    }

    @Path("greet")
    @GET
    public String doGreet() {
        return "Hello Stranger, the time is " + new Date();
    }

}

when I am hitting the url :

http://localhost:8080/pps/rest/todo/greet 

I am getting 404 on glassfish, I have ensured my glassfish is running by hitting the url of the testservlet.

Upvotes: 0

Views: 1440

Answers (2)

jjd
jjd

Reputation: 2288

Probably your problem is related to the Jersey version that is bundled with GAS 4.

Depends on exact version of your GAS you use some of Jersey 2.x version. As I can assume from your code you are initializing jersey the way it should be done for version 1.x. Take a look at Arun's example here. You can download sources for the example on that page. His example is too basic though and does not even have web.xml file at all(because it is a very basic example and web.xml is now optional). That's why you might also want to take a look at jersey sample applications code. For instance bookstore could be a good example.

In two words you have to extend org.glassfish.jersey.server.ResourceConfig

@ApplicationPath("/")
public class MyApplication extends ResourceConfig {
       public MyApplication() {
          registerClasses(UsersResource.class);
          register(new JettisonFeature());
       }
}

and provide it as javax.ws.rs.Application parameter for Jersey Servlet or Filter based on your choice. It is going to be filter in bookstore example. You will see in the code. There are plenty examples in the maven repo explaining more advanced stuff

    <filter>
        <filter-name>org.glassfish.jersey.examples.bookstore.webapp.MyApplication</filter-name>
        <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>org.glassfish.jersey.examples.bookstore.webapp.MyApplication</param-value>
        </init-param>
        <!-- pass to next filter if Jersey/App returns 404 -->
        <init-param>
            <param-name>jersey.config.servlet.filter.forwardOn404</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>org.glassfish.jersey.examples.bookstore.webapp.MyApplication</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Upvotes: 2

rj4u
rj4u

Reputation: 97

I think the reason for this is that you have not specified the path correctly in your code. All I see is paths like /todo , /todo1, /greet. What you are looking for is /todo/greet, so this should be specified in the WebService method @Path annotation.

Annotate the method that you want to use as webservice like this : @Path("/todo/greet")

The reason you are getting 404 is that there is no method in your class with the path /todo/greet.

Hope this helps!

Thanks

Upvotes: 0

Related Questions