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>

你对…的定义

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

表示不创建JettyTest就不能创建TestApplication

只需将内部类更改为static,这应该就足够了。

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

相关内容

  • 没有找到相关文章

最新更新