Vikas Cheruku
Vikas Cheruku

Reputation: 9

REST APIs return 404 NOT FOUND when path configured in web.xml

I configured a REST application using Jersey in eclipse.

I am unable to send REST requests when the path in web.xml is configured as /*, but when I change it to /rest/*, I get a 404 NOT FOUND error. There are no exceptions at the server.

The web.xml file is as shown :

<servlet>
    <servlet-name>Jersey Web Application</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.app.user</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>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

Here is how I declared the Path in the java file

@Path("/rest/products")
    public class Product {

I am getting a 404 error when I access the path /rest/products on the server URL.

What am I missing?

Help is greatly appreciated!

Upvotes: 0

Views: 3095

Answers (2)

swayamraina
swayamraina

Reputation: 3158

You have mapped your servlet to " /rest/* " url i.e. whenever there is a request with a url of ......./rest/* your servlet ServletContainer will be called to handle it.

in your java file @Path("/rest/products") is mentioned.

you are getting this 404 error because of the forward slash in the path mentioned in @Path. This is because when you give path starting with forward slash it takes it as absolute path and not relative.

so your final url will not end up like /myProject/rest/products but instead will look like /rest/products which it is unable to find.

Hence the error.

Upvotes: 0

Aditya Garimella
Aditya Garimella

Reputation: 933

As you mapped your Jersey Web Application to /rest/* all the requests are supposed to have /rest in their path. You mapped your Product class to /rest/products so the entire url should be http://localhost:port/contextRoot/rest/rest/products. If you don't want rest two times in the url just map the Product class to /products.

Upvotes: 1

Related Questions