RESTEASY003900:无法找到 public 构造函数 - 我缺少什么?

RESTEASY003900: Unable to find a public constructor - What am I missing?

在另一个项目中,我有一个嵌入式 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)

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

谢谢

我的测试class:

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>

你对...的定义

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

表示您必须先创建 JettyTest

才能创建 TestApplication

把里面的class改成static就可以了

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