Tomcat 8.0为泽西岛休息服务提供404

时间:2016-09-09 05:51:55

标签: java rest maven tomcat jersey

我是Java的新手,我一直在尝试在Tomcat 8.0上使用Jersey 2.23创建一个休息服务 我已经阅读了相同的许多其他帖子,相应地更改了它,我仍然得到同样的错误。

enter image description here

网址: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。 我现在收到这个错误。这似乎更相关,但我无法理解这个问题。

    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)

这是我的第一个项目,我还没有能够运行一件事。请帮忙。

2 个答案:

答案 0 :(得分:0)

网址应为

  

网址: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

答案 1 :(得分:0)

路径应为http://localhost:8080/ app_name / api / GetStarted。 app_name 是Eclipse中的项目名称。检查工作区下的 .metadata.plugins \ org.eclipse.wst.server.core \ tmp0 | tmp1 \ wtpwebapps 目录,看看启动服务器后是否真的部署了应用程序(假设您使用的是wtp) 。如果它不在那里,那么您有错误或者您没有将您的应用程序添加到服务器。如果有,请检查名称。