有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java RESTEASY003900:找不到公共构造函数我缺少什么?

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

共 (1) 个答案

  1. # 1 楼答案

    你对…的定义

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

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

    只要将内部类更改为static,就足够了

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