error404使用jersey框架和tomcat服务器访问Restful Web服务

时间:2017-08-22 09:34:59

标签: java eclipse rest jersey tomcat7

通过此网址访问Restful Web服务时遇到问题: localhost:8080/PrimNumbers/Generator/strategy

我收到了这个错误:

  

HTTP状态404 - / PrimNumbers / Generator / strategy

     

输入状态报告

     

message / PrimNumbers / Generator / strategy

     

说明请求的资源不可用。

使用tomcat7.0.81服务器和jersey1.17框架(我在我的项目中包含了WEB-INF / lib下的jersey jar文件)。 这是我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<element>
<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_3_0.xsd" 
version="3.0">
  <display-name>PrimNumbers</display-name>
   <servlet> 
      <servlet-name>Jersey RESTful Application</servlet-name> 
      <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
      <init-param> 
         <param-name> jersey.config.server.provider.packages</param-name> 
         <param-value>PrimeNbs</param-value> 
      </init-param> 
   </servlet> 
   <servlet-mapping> 
      <servlet-name>Jersey RESTful Application</servlet-name> 
      <url-pattern>/*</url-pattern> 
   </servlet-mapping>   
</web-app>
</element

&GT;

这是我的service.java(我只测试服务):

package PrimeNbs;

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


//annotations
@Path("/Generator")

public class PrimeNbsGeneratorService {

    @GET
    @Path("/strategy")
    @Produces(MediaType.TEXT_HTML)  
    public String Strategy1(){
        boolean prime=true;
        String rslt="<HTML><h1> Prime numbers: ";
        return(rslt+" test...</h1></HTML>");
    }
}

在运行我的服务器时,我得到了这些日志消息:

> ug 22, 2017 11:22:44 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:PrimNumbers' did not find a matching property.
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/7.0.81
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Aug 11 2017 10:21:27 UTC
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         7.0.81.0
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Linux
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            4.4.0-21-generic
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             /home/elhem/java/jre1.8.0_121
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_121-b13
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /home/elhem/workspace/tomcat
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /home/elhem/workspace/tomcat
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/home/elhem/workspace/tomcat
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/home/elhem/workspace/tomcat
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/home/elhem/workspace/tomcat/wtpwebapps
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/home/elhem/workspace/tomcat/endorsed
Aug 22, 2017 11:22:44 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Aug 22, 2017 11:22:44 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Aug 22, 2017 11:22:45 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 22, 2017 11:22:45 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Aug 22, 2017 11:22:45 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 865 ms
Aug 22, 2017 11:22:45 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 22, 2017 11:22:45 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.81
Aug 22, 2017 11:22:45 AM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Aug 22, 2017 11:22:45 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/elhem/workspace/tomcat/webapps/host-manager
Aug 22, 2017 11:22:45 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /home/elhem/workspace/tomcat/webapps/host-manager has finished in 74 ms
Aug 22, 2017 11:22:46 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/elhem/workspace/tomcat/webapps/manager
Aug 22, 2017 11:22:46 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /home/elhem/workspace/tomcat/webapps/manager has finished in 67 ms
Aug 22, 2017 11:22:46 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/elhem/workspace/tomcat/webapps/ROOT
Aug 22, 2017 11:22:46 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /home/elhem/workspace/tomcat/webapps/ROOT has finished in 61 ms
Aug 22, 2017 11:22:46 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/elhem/workspace/tomcat/webapps/docs
Aug 22, 2017 11:22:46 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /home/elhem/workspace/tomcat/webapps/docs has finished in 60 ms
Aug 22, 2017 11:22:46 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/elhem/workspace/tomcat/webapps/examples
Aug 22, 2017 11:22:46 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Aug 22, 2017 11:22:46 AM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Aug 22, 2017 11:22:46 AM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@51542878')
Aug 22, 2017 11:22:46 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /home/elhem/workspace/tomcat/webapps/examples has finished in 231 ms
Aug 22, 2017 11:22:46 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 22, 2017 11:22:46 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Aug 22, 2017 11:22:46 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1397 ms

任何人都可以帮忙!我会非常感激的。

1 个答案:

答案 0 :(得分:0)

您的web.xml存在问题。

现在,您创建一个名为Jersey RESTful Application的servlet和一个名为PrimNumbers的servlet的映射。但是servler PrimNumbers不存在。

所以你需要做的是使servletand servlet-mapping使用相同的名称。

像这样:

  <servlet> 
      <servlet-name>Jersey RESTful Application</servlet-name> 
      <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
      <init-param> 
         <param-name> jersey.config.server.provider.packages</param-name> 
         <param-value>PrimeNbs</param-value> 
      </init-param> 
  </servlet> 
  <servlet-mapping> 
      <servlet-name>Jersey RESTful Application</servlet-name> 
      <url-pattern>/*</url-pattern> 
  </servlet-mapping>