javax.servlet.ServletException: Servlet.init() 对于 servlet resteasy-servlet 抛出异常

javax.servlet.ServletException: Servlet.init() for servlet resteasy-servlet threw exception

输入异常报告

message

description The server encountered an internal error () that prevented it from >fulfilling this request.

   >exception

  >javax.servlet.ServletException: Servlet.init() for servlet resteasy-servlet >threw exception
org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
java.lang.Thread.run(Thread.java:745)

根本原因

   >java.lang.RuntimeException: Failed to construct public com.ph.restful.main.StringServiceApplication()
org.jboss.resteasy.core.ConstructorInjectorImpl.construct(ConstructorInjectorImpl.java:144)
org.jboss.resteasy.spi.ResteasyDeployment.createFromInjectorFactory(ResteasyDeployment.java:282)
org.jboss.resteasy.spi.ResteasyDeployment.createApplication(ResteasyDeployment.java:259)
org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:220)
org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.init(ServletContainerDispatcher.java:67)
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.init(HttpServletDispatcher.java:36)
org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
java.lang.Thread.run(Thread.java:745)

根本原因

   >java.lang.WhosebugError
java.util.HashMap.init(HashMap.java:330)
java.util.HashMap.<init>(HashMap.java:262)
java.util.HashMap.<init>(HashMap.java:281)
java.util.HashSet.<init>(HashSet.java:103)
com.ph.restful.main.StringServiceApplication.<init>(StringServiceApplication.java:12)
com.ph.restful.main.StringServiceApplication.<init>(StringServiceApplication.java:15)
com.ph.restful.main.StringServiceApplication.<init>(StringServiceApplication.java:15)
com.ph.restful.main.StringServiceApplication.<init>(StringServiceApplication.java:15)
com.ph.restful.main.StringServiceApplication.<init>(StringServiceApplication.java:15)
com.ph.restful.main.StringServiceApplication.<init>(StringServiceApplication.java:15)
com.ph.restful.main.StringServiceApplication.<init>(StringServiceApplication.java:15)
com.ph.restful.main.StringServiceApplication.<init>(StringServiceApplication.java:15)
com.ph.restful.main.StringServiceApplication.<init>(StringServiceApplication.java:15)

当我将浏览器用作 http://localhost:8080/sample/rest/app/test 时出现错误,这将 return 一个 json 数据类型..

产品class

private String productname;
private String description;
private Integer price;

public String getProductname() {
    return productname;
}
public void setProductname(String productname) {
    this.productname = productname;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public Integer getPrice() {
    return price;
}
public void setPrice(Integer price) {
    this.price = price;
}

字符串服务class

    @Path("/app")
    public class StringService {

     @GET
     @Path("/test")
     @Produces("application/json")
     public Product printSample2(){
             Product prod = new Product();
             prod.setProductname("Alaska Milk");
             prod.setDescription("Milk for all");
             prod.setPrice(300);
            return prod;
     }
  }

StringServiceApplication

        public class StringServiceApplication extends Application{

        private Set<Object> singletons = new HashSet<Object>();

        public StringServiceApplication(){
                  singletons.add(new StringServiceApplication());
         }

        @Override
        public Set<Object> getSingletons(){
                  return singletons;
        }

     }

我的web.xml

<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/rest</param-value>
</context-param>

<servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>
        org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
            </servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.ph.restful.main.StringServiceApplication</param-value>
    </init-param>
</servlet>

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

我的jboss-web.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <jboss-web>
            <context-root>sample</context-root>
      </jboss-web>

我在构建路径上的库 -javassist-3.12.1.GA -json-简单-1.1.1 -resteasy-jaxb-provider-2.2.1.ga -resteasy-jaxrs-2.2.1.GA -resteasy-jettison-provider -scannotation-1.0.3

java.lang.WhosebugError

public class StringServiceApplication extends Application{
    public StringServiceApplication(){
        singletons.add(new StringServiceApplication());
    }
}

您正在堆栈溢出,因为您试图在其自己的构造函数中实例化相同的 class。

我想你的意思是实例化 StringService 而不是

public StringServiceApplication(){
    singletons.add(new StringService());
}