Search code examples
javaapacherestjax-rsweb.xml

Reduce URI path in REStful with Java (JAX-RS)


I am newbie to java and JAX-RS.i have developed working example for simple application.

now i want to reduce my url in RESTFul.

Currently i have http://localhost:8084/eshipper_api/apis/api/get_quote this url.

its working and giving output.

i want to reduce it and want to make it like http://localhost:8084/eshipper_api/apis/get_quote

My web.xml

     <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
         <servlet>
           <servlet-name>Eshipper API 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.eshipper.client.request</param-value>
              </init-param>
           </servlet>
        <servlet-mapping>
        <servlet-name>Eshipper API Application</servlet-name>
           <url-pattern>/apis/*</url-pattern>
        </servlet-mapping>  
     </web-app>

Code in /java/com/eshipper/client/request/RequestHandler.java which is param value for com.eshipper.client.request

        @Path("api")
        public class RequestHandler 
        {
           @GET
           @Path("/get_quote")
           public String GetQuote()
           {
               return "GOdddd Working";
           }
        }

Please help me for this. Thanks in advance.


Solution

  • Change your @Path("api") to @Path("")

        @Path("")
        public class RequestHandler 
        {
           @GET
           @Path("/get_quote")
           public String GetQuote()
           {
               return "GOdddd Working";
           }
        }
    

    Alternatively, you could change your web.xml to:

    <url-pattern>/*</url-pattern> and set your @Path("/apis") and it would do the same thing.

    You have your http://servername/context/web.xml servlet path/class annotated path/method annotated path