Shruti
Shruti

Reputation: 75

jersey rest service with tomcat not working with welcome file configuration in web.xml

I am trying to learn jersey rest service with tomcat but facing issues when trying to render index.html and then making ajax request to call the jersey service. WebContent

Here is my web.xml

<?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</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>


    <servlet>
        <servlet-name>Rest With Jersey2</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.rest.jersey2</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Rest With Jersey2</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

And this is my jersey service

package com.rest.jersey2;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

@Path("/hello")
public class Test {

    @GET
    @Produces("application/json")
    public Response defaultReverser()  {

        String result = "" ;
        return Response.status(200).entity(result).build();
    }


}

which i want to access from first.js that is included in index.html to request

$.ajax({
        url: "/hello",
        type: "GET",
        success: function(){ 
            alert("success");
        },
        error: function(){ 
            alert("error!");
        } 
    });

When i run my rest application on tomcat server it renders index.html but gives 404 Not found after click on a button to request the ajax for url /hello to invoke jersey service. There is no error message in console and server starts normally.

Could you please guide me to make it work!

EDIT: If I test my rest service independently it is not working with url mapping /rest/* in web.xml. However it works if if i use /* ,but in this case i am not able to display welcome file.

When I try to render index.html with rest service, This page is not able to find the first.js that is included in index.html buti tried to put first.js in many directory including the same as source file of rest service.

Upvotes: 1

Views: 1032

Answers (1)

Nicolas Fontenele
Nicolas Fontenele

Reputation: 193

I think the first thing you can do is test your rest service independently. Once you see it working you check the URL and see if your javascript code is pointing to the exact path.

Since you are getting 'not found' - I would say you probably not targetting the right service URI. Maybe because you are passing "/hello" in jquery where your service is mapped under rest/* . (So probably /rest/hello is you URL). If not, use the full URL of you service to check(ex: localhost:8080/rest/rest/hello). Keep in mind URL is going to be host:port/app/mappedurl/serviceurl

If still have problems have look in this jersey service ( https://github.com/NicolasFonte/rentals/blob/master/src/main/java/nicolas/trial/rest/RentalResource.java ) being connect by JQuery in this sample (https://github.com/NicolasFonte/rentals/blob/master/src/main/webapp/js/rentals.js ) and you will pretty much solve it.

Upvotes: 1

Related Questions