ServletException:未指定servlet类

Dav*_*ton 6 java tomcat servlets

我本来应该是一个非常简单的“ Hello World” servlet,但无法使其正常工作。我正在使用Eclipse,Tomcat 8,Java 7和Servlet 3.1。

我看过许多教程和问题,但它们并没有完全帮助。我见过的大多数教程都谈到通过扩展HttpServlet创建servlet。我有那些工作。现在,我想尝试更简洁的注释方法。

我一直在参考本教程,但它还不完整,似乎有一些不正确或不完整的示例: 打包和部署RESTful Web服务

为什么未加载com.testing.service.MyApplication?

非常感谢您对此程序的运行提供任何帮助!

这是我的文件:

MyApplication.java

package com.testing.service;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("app")
public class MyApplication extends Application {
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(HelloWorldResource.class);
        return s;
    }
}
Run Code Online (Sandbox Code Playgroud)

HelloWorldResource.java

package com.testing.service;

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

@Path("/helloworld")
public class HelloWorldResource {
    @SuppressWarnings("unused")
    private static final long serialVersionUID = 1L;

    @GET
    @Produces("text/plain")
    public String sayHello() {
        return "Hello World!";
    }
}
Run Code Online (Sandbox Code Playgroud)

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
  <display-name>service</display-name>
  <servlet>
    <servlet-name>com.testing.service.MyApplication</servlet-name>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.testing.service.MyApplication</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

</web-app>
Run Code Online (Sandbox Code Playgroud)

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>com.testing</groupId>
  <artifactId>service</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
  <name>Rest Test</name>
  <dependencies>
    <dependency>
      <groupId>javax.ws.rs</groupId>
      <artifactId>javax.ws.rs-api</artifactId>
      <version>2.0.1</version>
    </dependency>
  </dependencies>  
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
          <configuration>
            <source>1.7</source>
            <target>1.7</target>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
Run Code Online (Sandbox Code Playgroud)

每当我运行Tomcat时,它都会显示以下错误:

INFO: Marking servlet com.testing.service.MyApplication as unavailable
Feb 05, 2015 3:28:55 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /service threw load() exception
javax.servlet.ServletException: No servlet class has been specified for servlet com.testing.service.MyApplication
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1041)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4944)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5230)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Run Code Online (Sandbox Code Playgroud)

Dav*_*ton 6

我在一个相关问题的评论中找到了答案。答案由Alvin Thompson提供。不幸的是,我没有足够的名声来投票给你答案。

如果您正在使用标准的Tomcat安装(或其他一些servlet容器),则需要包括REST实现,因为Tomcat没有附带该实现。如果您使用的是Maven,请将其添加到“依赖项”部分:

<dependencies>
  <dependency>
    <groupId>org.glassfish.jersey.bundles</groupId>
    <artifactId>jaxrs-ri</artifactId>
    <version>2.13</version>
  </dependency>
  ...
</dependencies>
Run Code Online (Sandbox Code Playgroud)

然后只需将应用程序配置类添加到您的项目中。如果除了设置其余服务的上下文路径之外,没有其他特殊配置需求,则该类可以为空。添加此类后,您无需在web.xml中进行任何配置(或完全没有任何配置)。

最初发布在这里:

如何仅使用注释(不使用web.xml)设置JAX-RS Application?