2016-02-19 287 views
4

嗨試圖寫一個簡單的休息Web服務。和下面的教程如下: http://www.tutorialspoint.com/restful/index.htm實例化servlet類錯誤org.glassfish.jersey.servlet.ServletContainer

但我正在逐漸 HTTP狀態500 - 錯誤實例化servlet類org.glassfish.jersey.servlet.ServletContainer

例外:

<body> 
     <h1>HTTP Status 500 - Error instantiating servlet class org.glassfish.jersey.servlet.ServletContainer</h1> 
     <div class="line"></div> 
     <p> 
      <b>type</b> Exception report 
     </p> 
     <p> 
      <b>message</b> 
      <u>Error instantiating servlet class org.glassfish.jersey.servlet.ServletContainer</u> 
     </p> 
     <p> 
      <b>description</b> 
      <u>The server encountered an internal error that prevented it from fulfilling this request.</u> 
     </p> 
     <p> 
      <b>exception</b> 
     </p> 
     <pre>javax.servlet.ServletException: Error instantiating servlet class org.glassfish.jersey.servlet.ServletContainer 
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) 
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522) 
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095) 
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672) 
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) 
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) 
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    java.lang.Thread.run(Thread.java:745) 
</pre> 
     <p> 
      <b>root cause</b> 
     </p> 
     <pre>java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer 
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308) 
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1142) 
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 

以下是代碼(與教程大部分相同)

package com.abhi; 

import java.util.List; 

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

@Path("/UserService") 
public class UserService { 

    UserDao userDao = new UserDao(); 

    @GET 
    @Path("/users") 
    @Produces(MediaType.APPLICATION_XML) 
    public List<User> getUsers(){ 
     return userDao.getAllUsers(); 
    }  
} 

My Web.x ml:

<?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" 
    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>MyRestPrjct</display-name> 
    <servlet> 
     <servlet-name>Jersey RESTful Application</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.abhi</param-value> 
     </init-param> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Jersey RESTful Application</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 
    <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> 
</web-app> 
+1

通常這是由於包含Jersey 1.x jar或不在WEB-INF/lib文件夾中包含這些jar。 –

回答

5

我只是沒有正確放置罐子。我沒有把所需的罐子放在webapps\MyRestPrjct\WEB-INF\lib中,而是將它放在webapps\MyRestPrjct\WEB-INF\lib\libs中。

我把罐子從libs移到lib後工作正常。

3

將所有jar放在WEB-INF/lib文件夾中解決了我的問題。

1

我也遇到了類似的問題,當我做了同樣的教程中,我發現了問題是在web.xml中指定的servelt類

org.glassfish.jersey.servlet.ServletContainer 

,但是當我在罈子裏檢查有沒有包在具有上述路徑的庫中,因此我將其更改爲現有的庫

com.sun.jersey.spi.container.servlet.ServletContainer 

並解決了問題。

相關問題