简体   繁体   中英

simple RESTful web services in java(JAX-RS) using jersey , throws HTTP Status 404

When i am running my simple Java code for RESTful Web Services (JAX-RS), it throws 404 error. I have mapped it will in web.xml and have no compilation error. One suspecios message i am getting in my log is " INFO: No provider classes found. ", please suggest me to eliminate this problem.

Console Log -:

May 31, 2014 10:23:27 AM org.apache.catalina.startup.HostConfig checkResources
INFO: Reloading context [/rest]
May 31, 2014 10:23:27 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
  stk5
May 31, 2014 10:23:27 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class stk5.ConversionService
May 31, 2014 10:23:27 AM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
May 31, 2014 10:23:27 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.18 11/22/2013 01:21 AM'

my ConversionService.java file -:

package stk5;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("ConversionService")


public class ConversionService{
     @GET
     @Path("/InchToFeet/{i}")
      @Produces(MediaType.TEXT_XML)
      public String convertInchToFeet(@PathParam("i") int i) {

        int inch=i;
        double feet = 0;
        feet =(double) inch/12;

        return "<InchToFeetService>"
        + "<Inch>" + inch + "</Inch>"
          + "<Feet>" + feet + "</Feet>"
         + "</InchToFeetService>";
      }

      @Path("/FeetToInch/{f}")
      @GET
      @Produces(MediaType.TEXT_XML)
      public String convertFeetToInch(@PathParam("f") int f) {
       int inch=0;
          int feet = f;
          inch = 12*feet;

          return "<FeetToInchService>"
            + "<Feet>" + feet + "</Feet>"
            + "<Inch>" + inch + "</Inch>"
            + "</FeetToInchService>";
      }
}

my Web.xml file -:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>restApp</display-name>
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>stk5</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app> 

What URL are you going to in order to test it? The context is /rest and your path is /InchToFeet/{i} , so are you going to /rest/InchToFeet/<num> ?

Edit: I also noticed you have a @Path("ConversionService") on the class. That would make your URL /restConversionService/InchToFeet/<num> . You might want to precede the ConversionService with a slash to make it /rest/ConversionService/InchToFeet/<num> .

Instead of mentioning the URL pattern in web.xml I would recommend you to put this in your Service @Path .

You should so consider your context root, if you have not set it then by default it is your Deployment (war/ear/jar etc) name.

So modify your <url-pattern>/rest/*</url-pattern> to <url-pattern>/*</url-pattern> . Now your URL should look something like this http://localhost:8080/[context root]/ConversionService/InchToFeet/2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM