HTTP Status 500 javax.servlet.ServletException: Servlet.init() for servlet [Jersey RESTful App] threw exception java.lang.IllegalArgumentException

huangapple go评论74阅读模式
英文:

HTTP Status 500 javax.servlet.ServletException: Servlet.init() for servlet [Jersey RESTful App] threw exception java.lang.IllegalArgumentException

问题

<br>
<br>
我正在编写我的第一个Java REST应用程序。<br>
我正在遵循这个教程:<br>
https://www.tutorialspoint.com/restful/restful_first_application.htm<br>
但我遇到了这个错误:<br>

    HTTP状态500 - 内部服务器错误
    
    类型异常报告
    
    消息Servlet.init()对于servlet [Jersey RESTful Application]引发了异常
    
    描述服务器遇到了一个意外的情况,阻止了它无法完成请求。
    
    异常
    
    javax.servlet.ServletException:对于servlet [Jersey RESTful Application]的Servlet.init()引发了异常
    	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    	org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    	org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
    	org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    	org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
    	org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    	java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    	java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    	java.base/java.lang.Thread.run(Thread.java:830)
    
    根本原因
    
    java.lang.IllegalArgumentException
    	jersey.repackaged.org.objectweb.asm.ClassReader.<init>(ClassReader.java:171)
    	jersey.repackaged.org.objectweb.asm.ClassReader.<init>(ClassReader.java:153)
    	jersey.repackaged.org.objectweb.asm.ClassReader.<init>(ClassReader.java:425)
    	org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener.process(AnnotationAcceptingListener.java:170)
    	org.glassfish.jersey.server.ResourceConfig.scanClasses(ResourceConfig.java:915)
    	org.glassfish.jersey.server.ResourceConfig._getClasses(ResourceConfig.java:869)
    	org.glassfish.jersey.server.ResourceConfig.getClasses(ResourceConfig.java:775)
    	org.glassfish.jersey.server.ResourceConfig$RuntimeConfig.<init>(ResourceConfig.java:1206)
    	org.glassfish.jersey.server.ResourceConfig$RuntimeConfig.<init>(ResourceConfig.java:1178)
    	org.glassfish.jersey.server.ResourceConfig.createRuntimeConfig(ResourceConfig.java:1174)
    	org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:345)
    	org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392)
    	org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)
    	org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369)
    	javax.servlet.GenericServlet.init(GenericServlet.java:158)
    	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    	org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    	org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
    	org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    	org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
    	org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    	java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    	java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    	java.base/java.lang.Thread.run(Thread.java:830)

<br>
你能帮我吗?<br>
我查看了许多类似的主题和文章,还在Stack Overflow上查看了许多内容,但没有帮助我解决问题。<br>
<br>
我的规格:<br>
操作系统:Windows 10<br>
IDE:Eclipse版本:2020-06(4.16.0)<br>
应用服务器:apache-tomcat-9.0.38<br>
jaxrs-ri-2.25.1<br>
<br>
<br>
我将代码从教程网站复制到我的项目中,以确保我没有错误。<br>
Java文件:<br>
UserService.java<br>

    package com.tutorialspoint;  
    
    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(); 
       }  
    }
<br>
User.java<br>

    package com.tutorialspoint;  
    
    import java.io.Serializable;  
    import javax.xml.bind.annotation.XmlElement; 
    import javax.xml.bind.annotation.XmlRootElement; 
    @XmlRootElement(name = "user") 
    
    public class User implements Serializable {  
       private static final long serialVersionUID = 1L; 
       private int id; 
       private String name; 
       private String profession;  
       public User(){} 
        
       public User(int id, String name, String profession){  
          this.id = id; 
          this.name = name; 
          this.profession = profession; 
       }  
       public int getId() { 
          return id; 
       }  
       @XmlElement 
       public void setId(int id) { 
          this.id = id; 
       } 
       public String getName() { 
          return name; 
       } 
       @XmlElement
       public void setName(String name

<details>
<summary>英文:</summary>

&lt;br&gt;
&lt;br&gt;
I am writing my first Java REST application.&lt;br&gt;
I was following this tutorial:&lt;br&gt;
https://www.tutorialspoint.com/restful/restful_first_application.htm&lt;br&gt;
but I am getting this error:&lt;br&gt;

    HTTP Status 500 – Internal Server Error
    
    Type Exception Report
    
    Message Servlet.init() for servlet [Jersey RESTful Application] threw exception
    
    Description The server encountered an unexpected condition that prevented it from fulfilling the request.
    
    Exception
    
    javax.servlet.ServletException: Servlet.init() for servlet [Jersey RESTful Application] threw exception
    	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    	org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    	org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
    	org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    	org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
    	org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    	java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    	java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    	java.base/java.lang.Thread.run(Thread.java:830)
    
    Root Cause
    
    java.lang.IllegalArgumentException
    	jersey.repackaged.org.objectweb.asm.ClassReader.&lt;init&gt;(ClassReader.java:171)
    	jersey.repackaged.org.objectweb.asm.ClassReader.&lt;init&gt;(ClassReader.java:153)
    	jersey.repackaged.org.objectweb.asm.ClassReader.&lt;init&gt;(ClassReader.java:425)
    	org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener.process(AnnotationAcceptingListener.java:170)
    	org.glassfish.jersey.server.ResourceConfig.scanClasses(ResourceConfig.java:915)
    	org.glassfish.jersey.server.ResourceConfig._getClasses(ResourceConfig.java:869)
    	org.glassfish.jersey.server.ResourceConfig.getClasses(ResourceConfig.java:775)
    	org.glassfish.jersey.server.ResourceConfig$RuntimeConfig.&lt;init&gt;(ResourceConfig.java:1206)
    	org.glassfish.jersey.server.ResourceConfig$RuntimeConfig.&lt;init&gt;(ResourceConfig.java:1178)
    	org.glassfish.jersey.server.ResourceConfig.createRuntimeConfig(ResourceConfig.java:1174)
    	org.glassfish.jersey.server.ApplicationHandler.&lt;init&gt;(ApplicationHandler.java:345)
    	org.glassfish.jersey.servlet.WebComponent.&lt;init&gt;(WebComponent.java:392)
    	org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)
    	org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369)
    	javax.servlet.GenericServlet.init(GenericServlet.java:158)
    	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    	org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    	org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
    	org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    	org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
    	org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    	java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    	java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    	java.base/java.lang.Thread.run(Thread.java:830)

&lt;br&gt;
Could you help me?&lt;br&gt;
I checked with many similiar topics and articles also here on Stack Overflow but nothing helped me.&lt;br&gt;
&lt;br&gt;
My specification:&lt;br&gt;
OS: Windows 10&lt;br&gt;
IDE: Eclipse Version: 2020-06 (4.16.0)&lt;br&gt;
Application server: apache-tomcat-9.0.38&lt;br&gt;
jaxrs-ri-2.25.1&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I copied code from tutorial website to my project to make sure I made no mistake.&lt;br&gt;
Java files:&lt;br&gt;
UserService.java&lt;br&gt;

    package com.tutorialspoint;  
    
    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(&quot;/UserService&quot;) 
    
    public class UserService {  
       UserDao userDao = new UserDao();  
       @GET 
       @Path(&quot;/users&quot;) 
       @Produces(MediaType.APPLICATION_XML) 
       public List&lt;User&gt; getUsers(){ 
          return userDao.getAllUsers(); 
       }  
    }
&lt;br&gt;
User.java&lt;br&gt;

    package com.tutorialspoint;  
    
    import java.io.Serializable;  
    import javax.xml.bind.annotation.XmlElement; 
    import javax.xml.bind.annotation.XmlRootElement; 
    @XmlRootElement(name = &quot;user&quot;) 
    
    public class User implements Serializable {  
       private static final long serialVersionUID = 1L; 
       private int id; 
       private String name; 
       private String profession;  
       public User(){} 
        
       public User(int id, String name, String profession){  
          this.id = id; 
          this.name = name; 
          this.profession = profession; 
       }  
       public int getId() { 
          return id; 
       }  
       @XmlElement 
       public void setId(int id) { 
          this.id = id; 
       } 
       public String getName() { 
          return name; 
       } 
       @XmlElement
       public void setName(String name) { 
          this.name = name; 
       } 
       public String getProfession() { 
          return profession; 
       } 
       @XmlElement 
       public void setProfession(String profession) { 
          this.profession = profession; 
       }   
    } 
UserDao.java&lt;br&gt;

    package com.tutorialspoint;  
    
    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileNotFoundException;  
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.ObjectInputStream; 
    import java.io.ObjectOutputStream; 
    import java.util.ArrayList; 
    import java.util.List;  
    
    public class UserDao { 
       public List&lt;User&gt; getAllUsers(){ 
          
          List&lt;User&gt; userList = null; 
          try { 
             File file = new File(&quot;Users.dat&quot;); 
             if (!file.exists()) { 
                User user = new User(1, &quot;Mahesh&quot;, &quot;Teacher&quot;); 
                userList = new ArrayList&lt;User&gt;(); 
                userList.add(user); 
                saveUserList(userList); 
             } 
             else{ 
                FileInputStream fis = new FileInputStream(file); 
                ObjectInputStream ois = new ObjectInputStream(fis); 
                userList = (List&lt;User&gt;) ois.readObject(); 
                ois.close(); 
             } 
          } catch (IOException e) { 
             e.printStackTrace(); 
          } catch (ClassNotFoundException e) { 
             e.printStackTrace(); 
          }   
          return userList; 
       } 
       private void saveUserList(List&lt;User&gt; userList){ 
          try { 
             File file = new File(&quot;Users.dat&quot;); 
             FileOutputStream fos;  
             fos = new FileOutputStream(file); 
             ObjectOutputStream oos = new ObjectOutputStream(fos); 
             oos.writeObject(userList); 
             oos.close(); 
          } catch (FileNotFoundException e) { 
             e.printStackTrace(); 
          } catch (IOException e) { 
             e.printStackTrace(); 
          } 
       }    
    }

web.xml&lt;br&gt;

    &lt;?xml version = &quot;1.0&quot; encoding = &quot;UTF-8&quot;?&gt; 
    &lt;web-app xmlns:xsi = &quot;http://www.w3.org/2001/XMLSchema-instance&quot;  
       xmlns = &quot;http://java.sun.com/xml/ns/javaee&quot;  
       xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee  
       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&quot;  
       id = &quot;WebApp_ID&quot; version = &quot;3.0&quot;&gt; 
       &lt;display-name&gt;User Management&lt;/display-name&gt; 
       &lt;servlet&gt; 
          &lt;servlet-name&gt;Jersey RESTful Application&lt;/servlet-name&gt; 
          &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt; 
          &lt;init-param&gt; 
             &lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt; 
             &lt;param-value&gt;com.tutorialspoint&lt;/param-value&gt; 
          &lt;/init-param&gt; 
       &lt;/servlet&gt; 
       &lt;servlet-mapping&gt; 
          &lt;servlet-name&gt;Jersey RESTful Application&lt;/servlet-name&gt; 
          &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt; 
       &lt;/servlet-mapping&gt;   
    &lt;/web-app&gt;

</details>


# 答案1
**得分**: 0

我使用 apache-tomcat-9.0.38 和 Jersey 2.16 使其工作。

<details>
<summary>英文:</summary>

I got it working by using apache-tomcat-9.0.38 and Jersey 2.16

</details>



huangapple
  • 本文由 发表于 2020年10月4日 17:58:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64193240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定