3

I have created a simple restful web service based on some examples and built it into a .war file(project structure has web.xml under WEB-INF), deploy it on glassfish ang get a 404 not found error when i try to call it. My class containing the service is :

 import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; /** * Created by Nikos Kritikos on 10/22/2015. */ @Path("/decks") public class HS_Services { @Path("sayHello/{name}") @GET public String doSayHello(@PathParam("name") String name) { return "Hello there "+name; } } 

my web.xml is this :

<servlet> <servlet-name>HSRestServices</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>HSRestServices</servlet-name> <url-pattern>/hsrest/*</url-pattern> </servlet-mapping> 

i try to call it with http://localhost:8080/HSRestServices/hsrest/decks/sayHello/Nikos but i get 404 from glassfish.. Any help would be much appreciated, thank you.

4
  • Why /HSRestServices in URL? Commented Oct 22, 2015 at 20:29
  • Add @Controller to your HS_Services class to tell Spring that is web controller Commented Oct 22, 2015 at 20:34
  • because HSRestServices is the servlet's name, and the web application name.I tested localhost:8080/hsrest/decks/sayHello/Nikos , didnt work Commented Oct 22, 2015 at 20:36
  • Valijon, i dont use spring, why need this? Commented Oct 22, 2015 at 20:38

1 Answer 1

5

You are missing the init-param where you specify which packages should be scanned for REST endpoint classes.

Change your web.xml to look like this:

<servlet> <servlet-name>HSRestServices</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>insert.packagename.where.your.class.is.here</param-value> </init-param> </servlet> 

Make sure to insert the name of the package which contains your class.

You don't need Spring for this.

There is also another way which works without web.xml. For details have a look at this question.

Sign up to request clarification or add additional context in comments.

Comments