web.xml中的servlet-class路径

时间:2014-09-10 11:02:37

标签: java xml rest servlets jersey

我正在关注http://www.vogella.com/tutorials/REST/article.html教程。

我已经通过名称“JerseyDemoProject”创建了项目(Dyanamic web项目),创建了一个名为“Package1”的包,并且我在这个包中编写了程序Hello.java(在教程中给出)。

我修改了我的web.xml,如下所示

<?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" 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" id="WebApp_ID" version="3.0">
  <display-name>JerseyDemoProject</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>HelloServlet</servlet-name>
    <servlet-class>Package1.Hello</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>Package1</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>


</web-app>

当我尝试“在服务器上运行”时,它给出了404-未找到错误。 我正在使用Tomcat服务器

Hello class:

package Package1;


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("/hello")
public class Hello {

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

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

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

}

Here is my project structure

更新:

xml文件:

<?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" 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" id="WebApp_ID" version="3.0">
  <display-name>JerseyDemoProject</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>HelloServlet</servlet-name>
  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
     <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
         <param-value>Package1</param-value>
      </init-param>

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


</web-app>

类文件

@Path("/hello")
public class Hello {

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

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

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

}

使用的网址

http://localhost:8080/JerseyDemoProject/rest/hello/inhtmlform

显示错误

HTTP Status 500 - Error instantiating servlet class com.sun.jersey.spi.container.servlet.ServletContainer

堆栈跟踪:

Exception:
javax.servlet.ServletException: Error instantiating servlet class com.sun.jersey.spi.container.servlet.ServletContainer
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)

Rootcause:

java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)

我在控制台中发现了警告

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 (x86)\Java\jre7\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:/Program Files (x86)/Java/jre7/bin/client;C:/Program Files (x86)/Java/jre7/bin;C:/Program Files (x86)/Java/jre7/lib/i386;C:\product\11.2.0\client_1\bin;C:\Informatica\961_989\961_Install\java\jre\bin\server;C:\Informatica\961_989\961_Install\tools\odbcdrv;C:\Informatica\961_989\961_Install\tools\jdbcdrv;C:\Informatica\961_989\961_Install\tools\datadirect;C:\app\vpalagum\product\11.2.0\client_2\bin;C:\app\vpalagum\product\11.2.0\client_1;C:\Program Files (x86)\RSA SecurID Token Common;C:\Program Files\Common Files\Microsoft Shared\Microsoft Online Services;C:\Program Files (x86)\Common Files\Microsoft Shared\Microsoft Online Services;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Box\Box Edit\;C:\Program Files (x86)\Java\jdk7_u15\bin;c:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Perforce;C:\Softwares\eclipse4.2;;.

WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:JerseyDemoProject' did not find a matching property.

4 个答案:

答案 0 :(得分:3)

因为指定的网址格式为

<url-pattern>/rest/*</url-pattern>

此外,您的servlet标记应该是这样的

<servlet>
  <servlet-name>HelloServlet</servlet-name>
  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
     <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
         <param-value>Package1</param-value>
      </init-param>
</servlet>

您可以在资源方法中指定其他路径,以便单独调用它,如

    @Path("/hello")
    public class Hello {

    @GET
    @Path("/intextform")
    @Produces(MediaType.TEXT_PLAIN)
    public String helloWorld(){
        return "Hello from Jersey!";

       }
   }

然后右键单击项目并在服务器上运行

试试这个网址

http://localhost:8080/JerseyDemoProject/rest/hello/intextform

答案 1 :(得分:2)

问题不在于Jersey 2.1,问题在于你的servlet类。 您应该在web.xml中使用以下servlet类(适用于所有Jersey 2.x应用程序)

<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

而不是:

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

然后一切都会正常。

最后,你的servlet应该是:

<servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>    
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>Package1</param-value>
    </init-param>
</servlet>

答案 2 :(得分:0)

首先请在您的web.xml中将<servlet-class>Package1.Hello</servlet-class>替换为<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-cl‌​ass>,然后重新运行该应用

请尝试以下网址:

http://localhost:8080/JerseyDemoProject/Package1/rest/hello

前提是您已将控制器映射到@PATH("/hello")

答案 3 :(得分:0)

我认为问题是因为我正在使用球衣2.1。我将其移除,从https://jersey.java.net/download.html安装了Jersey 1.18 JARbundle。我在WEB-INF lib文件夹中添加了这个jar,并在此文件夹中添加了asm.jar。 (这个jar可以在同一站点下载的Jersey1.18 Zip包中找到)。然后关注@SparkOn的答案。它工作了!