我是Java的新手,我一直试图在Tomcat 8.0上使用Jersey 2.23创建一个Rest服务。
我已经阅读了许多其他相同的文章,并相应地进行了更改,但仍然遇到相同的错误。

java - Tomcat 8.0提供404的Jersey休息服务-LMLPHP

网址:http://localhost:8080/RestCalculator/api/GetStarted

这是我的服务

package com.restcalculator;

import java.util.Date;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.InputStream;

@Path("/")
public class RestfulCalculator
{
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/GetStarted")
    public Response GetStarted()
    {
        String output = "RestfulCalculator Service is Running. Ping @" + new Date().toString();
        return Response.status(200).entity(output).build();
    }

}


我的POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>RestCalculator</groupId>
  <artifactId>RestCalculator</artifactId>
  <version>0.0.1</version>
  <packaging>war</packaging>
  <dependencies>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm-all</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-bundle</artifactId>
            <version>1.14</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
        </dependency>
    </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>


和我的web.xml

<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>RestCalculator</display-name>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>Jersey Rest Calculator 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>com.restcalculator</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Rest Calculator</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

</web-app>


我已经按照Jersey 2.23更新了servlet类名称。请告诉我我做错了什么。

编辑2:

固定的servlet名称,Tomcat服务器正在运行,并且index.html正在加载。
我现在收到此错误。似乎更相关,bt我无法理解问题。

    Sep 09, 2016 5:01:08 PM org.apache.jasper.servlet.TldScanner scanJars
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.
Sep 09, 2016 5:01:08 PM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet Jersey Rest Calculator Service as unavailable
Sep 09, 2016 5:01:08 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet [Jersey Rest Calculator Service] in web application [/RestCalculator] threw load() exception
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:518)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:499)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1027)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5038)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5348)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)


这是我的第一个项目,但我还无法运行任何东西。请帮忙。

最佳答案

该网址应为


  网址:http://localhost:8080/GetStarted


,如果您想访问GetStarted()方法。

除此之外,您还需要对代码进行如下调整:

package com.restcalculator;

import java.util.Date;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.InputStream;

@Path("/RestCalculator/api")
public class RestfulCalculator
{
    ...

}


这样您就可以使用相同的网址:

网址:


  http://localhost:8080/RestCalculator/api/GetStarted

10-07 19:52