【问题标题】:RESTEASY003900: Unable to find a public constructor - What am I missing?RESTEASY003900:找不到公共构造函数 - 我错过了什么?
【发布时间】:2021-10-10 12:03:08
【问题描述】:

在另一个项目中,我有一个嵌入式 Jetty 服务器,在将 Jetty 版本更新为 9.4.29 以上的任何版本后,我遇到了一个错误。 所以我去创建一个新项目,开始找出导致这个错误的原因。

但是...我什至没有遇到与新项目失败相同的错误:

javax.servlet.ServletException: org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher-7b2bbc3==org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher@b539e71f{jsp=null,order=-1,inst=true,async=true,src=EMBEDDED:null,STARTED}
    at jetty.test.JettyTest.hello(JettyTest.java:78)
Caused by: java.lang.IllegalArgumentException: RESTEASY003900: Unable to find a public constructor for provider class jetty.test.JettyTest$TestApplication
    at jetty.test.JettyTest.hello(JettyTest.java:78)

我感觉我显然遗漏了一些东西......感谢任何帮助。

谢谢

我的测试课:

package jetty.test;

import static io.restassured.RestAssured.given;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.ServletConfig;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.eclipse.jetty.server.Server;
import org.junit.jupiter.api.Test;

import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher;

public class JettyTest {
    @Path("/health")
    public class HealthResource {
        
        @GET
        @Path("/")
        @Produces(MediaType.TEXT_PLAIN)
        public Response getHealth() {
            return Response.ok().entity("good").build();
        }
    }

    @ApplicationPath("/api")
    public class TestApplication extends Application {
        HashSet<Object> singletons = new HashSet<>();

        public TestApplication() {
            super();
        }

        public TestApplication(@Context ServletConfig servletConfig) {
            singletons.add(HealthResource.class);
        }

        @Override
        public Set<Class<?>> getClasses() {
            HashSet<Class<?>> set = new HashSet<>();
            set.add(HealthResource.class);
            return set;
        }

        @Override
        public Set<Object> getSingletons() {
            return singletons;
        }
    }  
    
    @Test
    public void hello() throws Exception {
        Server server = new Server(8082);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);
       
        final ServletHolder servletHolder = new ServletHolder(new HttpServletDispatcher());
        servletHolder.setInitParameter("resteasy.scan", "true");        
        servletHolder.setInitParameter("resteasy.servlet.mapping.prefix", "/api/*");        
        servletHolder.setInitParameter("javax.ws.rs.Application", TestApplication.class.getName());

        context.addServlet(servletHolder, "/api/*");

        server.start();

        RequestSpecification spec = new RequestSpecBuilder()
                .setBaseUri("http://localhost:8082/").build();

        given().spec(spec).when().get("/api/res").then().statusCode(200);
    }
}

还有我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>org.test</groupId>
    <artifactId>JettyTestProject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>3.0.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.4.43.v20210629</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>9.4.43.v20210629</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.12.4</version>
        </dependency>
        
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-servlet-initializer</artifactId>
            <version>4.7.1.Final</version>
        </dependency>

    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
</project>

【问题讨论】:

    标签: java servlets jetty resteasy embedded-jetty


    【解决方案1】:

    你对……的定义

    public class JettyTest {
        ....
        public class TestApplication extends Application {
    

    意味着您不能在不先创建JettyTest 的情况下创建TestApplication

    把内部类改成静态就够了。

    public class JettyTest {
        ....
        public static class TestApplication extends Application {
    

    【讨论】:

    • 谢谢!我还需要 HealthResource 类上的静态修饰符。
    猜你喜欢
    • 2020-04-29
    • 2012-03-11
    • 1970-01-01
    • 2011-10-23
    • 2017-09-18
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    相关资源
    最近更新 更多