简体   繁体   中英

Rest Web service 404 error - Tomcat Eclipse

This is the first time I'm using Web Services, and it's driving me nuts.

I'm following the tutorial from : http://www.vogella.com/articles/REST/article.html - section 6

I created the Project Name as example.restservice.blue, and within that I have a package example.restservice.blue and within that I have a class Hey:

package example.restservice.blue;


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

// Plain old Java Object it does not extend as class or implements 
// an interface

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@Path("/Hey")
public class Hey {

// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey, please please work";
}

 // This method is called if XML is request
 @GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey, please please work" + "</hello>";
}

// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey, please please work" + "</title>"
    + "<body><h1>" + "Hello Jersey, please please work" + "</body></h1>" + "</html> ";
}

}

Above is the resources java 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>example.restservice.blue</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
 <servlet-name>Jersey REST Service</servlet-name>
 <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>example.restservice.blue</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>

Above is the web.xml file.

The project is named as example.restservice.blue as well.

The class file is present in example.restservice.blue/build/classes/example/restservice/blue/ The web.xml file is present is example.restservice.blue/WebContent/WEB-INF/

Tomcat 7 runs fine with another application and also displays the tomcat welcome page, so there are no issues with it, I think.

here is the error i get:

错误404

Here is the console:

>

Nov 07, 2014 5:34:37 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files (x86)\QuickTime\QTSystem\;.
Nov 07, 2014 5:34:38 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:example.restservice.bhagya' did not find a matching property.
Nov 07, 2014 5:34:38 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Nov 07, 2014 5:34:38 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Nov 07, 2014 5:34:38 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1608 ms
Nov 07, 2014 5:34:38 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Nov 07, 2014 5:34:38 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Nov 07, 2014 5:34:38 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(C:\Users\vaio\Desktop\RESTProject\wtpwebapps\example.restservice.blue\WEB-INF\lib\javax.servlet-api-3.0.1.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
Nov 07, 2014 5:34:42 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Users\vaio\Desktop\RESTProject\webapps\docs
Nov 07, 2014 5:34:42 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Users\vaio\Desktop\RESTProject\webapps\examples
Nov 07, 2014 5:34:42 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Nov 07, 2014 5:34:42 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Nov 07, 2014 5:34:42 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@53d4287')
Nov 07, 2014 5:34:42 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Users\vaio\Desktop\RESTProject\webapps\host-manager
Nov 07, 2014 5:34:42 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Users\vaio\Desktop\RESTProject\webapps\manager
Nov 07, 2014 5:34:42 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\Users\vaio\Desktop\RESTProject\webapps\ROOT
Nov 07, 2014 5:34:42 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Nov 07, 2014 5:34:42 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Nov 07, 2014 5:34:42 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4642 ms

Your configuration seems quite good but you don't access the correct page in your browser: if you pay attention in your tutorial it access to /rest/hello page not WEB-INF/classes/hello.java

In Java (or J2EE) you will never access a Java class: you will access a page that is mapped to a java controller (or web service in your case).

So to access your web service from browser, correct your URL to something like http://localhost:8080/example.restservice.blue/rest/Hey (for the web service with annotation @Path("/Hey"))

Before successfully deploy a web service RESTful, which was developed using JAX-RS, you must configure the REST resource path for your application. There are two ways to do this:

  • To use web.xml deployment descriptor
  • Develop a class that extends javax.ws.rs.core.Application and specify annotation @ApplicationPath

Example with using the deployment descriptor.

Class of web- service :

package example.restservice.blue;

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

@Path("Hey")
public class Hey {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
        return "Hello Jersey, please please work";
    }

    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + 
           "<hello> Hello Jersey, please please work" + "</hello>";
    }
}

Deployment descriptor:

<?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" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web- app_3_0.xsd" 
  id="WebApp_ID" 
  version="3.0">

<display-name>example.restservice.blue</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>JerseyService</servlet-name>
    <servlet-class>
       com.sun.jersey.spi.container.servlet.ServletContainer
    </servlet-class>

  <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>JerseyService</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

</web-app>

In this case you must make this request:

http://localhost:8080/root_context_here/rest/Hey

You can use the package scanning. As mentioned on the website vogella.com ,

The parameter com.sun.jersey.config.property.package defines in which package Jersey will look for the web service classes. This property must point to your resources classes. The URL pattern defines the part of the base URL your application will be placed.

In your case, you need to specify the correct URI in Path annotation and make the right request.

See Also:

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