excavator excavator - 1 month ago 10
reST (reStructuredText) Question

Building a REST web service

I am trying to build a simple Web service.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>REST_service</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Jersey</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>


This is my class:

package com.service.user;

import javax.ws.rs.*;

@Path("/user/service")
public class UserService
{
@PUT
@Path("/create")
public void createUser(){

}

@GET
public void getUser(){
System.out.println("Inside GET");
}
}


I am running it using this request: http://localhost:8080/REST_service/

And it starts fine. But when I go to call the GET request, it doesnt get called.
http://localhost:8080/REST_service/rest/user/service

I am very confused as to what's going wrong and not able to debug.

Any help appreciated!

Answer

You still need to tell jersey to scan your packages for your resource classes so it can register them.

<servlet>
    <servlet-name>Jersey</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>
            com.service
        </param-value>
    </init-param>
<servlet>