无法从导出的 JAR 中使用 Jetty、RestEasy 和 Guice 找到消息正文 reader
Could not find message body reader with Jetty, RestEasy and Guice from exported JAR
我有这个非常具体的问题。当来自 Eclipse 的 运行(没有任何参数)时,应用程序工作正常。但是给出
ERROR: RESTEASY002010: Failed to execute javax.ws.rs.NotSupportedException: RESTEASY003200: Could not find message body reader for type: class hu.edudroid.matek.web.endpoints.RequestData of content type: application/json
异常,当 运行ning 导出的 JAR (mvn clean compile assembly:single) 时。否则注入工作正常,不依赖 JSON 解析的端点工作正常。 get 方法工作正常,post 方法抛出错误。我不知道该怎么办。
我的服务器:
public static void main(String[] args) throws Exception {
Injector injector = Guice.createInjector(new MathModule(args));
int port = args.length > 0 ? Integer.parseInt(args[1]):8081;
Server server = new Server(port);
ServletContextHandler servletHandler = new ServletContextHandler();
servletHandler.addEventListener(injector.getInstance(GuiceResteasyBootstrapServletContextListener.class));
ServletHolder sh = new ServletHolder(HttpServletDispatcher.class);
servletHandler.addServlet(sh, "/*");
server.setHandler(servletHandler);
server.start();
server.join();
}
我的依赖项:
public class MathModule extends RequestScopeModule {
@Override
public void configure() {
super.configure();
bind(TestEndpoint.class).in(Singleton.class);
}
}
我的终点:
@Path("/test")
public class TestEndpoint {
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String get() {
return "Hello World";
}
@POST
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String post(RequestData value) {
return "Hello " + value.getName();
}
}
POJO:
@Data
@NoArgsConstructor
public class RequestData {
private String name;
}
最后 pom.xml:
<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>hu.edudroid.matek</groupId>
<artifactId>core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>core</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.8.47</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.5.v20170502</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.4.5.v20170502</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-guice</artifactId>
<version>3.1.3.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>hu.edudroid.matek.web.main.MatekServer</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
当您使用 jar-with-dependencies 程序集描述符时,所有依赖项 jar 都会提取到一个位置。有时这个罐子可以包含同名文件。在这种情况下,结果 jar 中只会包含 fist jar 中的文件。
在您的程序集文件中,META-INF\services\javax.ws.rs.ext.Providers 存在于两个依赖 jar 中(resteasy-jaxrs 和 resteasy-jettison-provider)。最后一个 jar 包含 application/json 内容类型的提供程序,但没有服务描述,resteasy 无法找到它。你可以试试 shade plugin with transformer which concatenate service description files
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
我有这个非常具体的问题。当来自 Eclipse 的 运行(没有任何参数)时,应用程序工作正常。但是给出
ERROR: RESTEASY002010: Failed to execute javax.ws.rs.NotSupportedException: RESTEASY003200: Could not find message body reader for type: class hu.edudroid.matek.web.endpoints.RequestData of content type: application/json
异常,当 运行ning 导出的 JAR (mvn clean compile assembly:single) 时。否则注入工作正常,不依赖 JSON 解析的端点工作正常。 get 方法工作正常,post 方法抛出错误。我不知道该怎么办。
我的服务器:
public static void main(String[] args) throws Exception {
Injector injector = Guice.createInjector(new MathModule(args));
int port = args.length > 0 ? Integer.parseInt(args[1]):8081;
Server server = new Server(port);
ServletContextHandler servletHandler = new ServletContextHandler();
servletHandler.addEventListener(injector.getInstance(GuiceResteasyBootstrapServletContextListener.class));
ServletHolder sh = new ServletHolder(HttpServletDispatcher.class);
servletHandler.addServlet(sh, "/*");
server.setHandler(servletHandler);
server.start();
server.join();
}
我的依赖项:
public class MathModule extends RequestScopeModule {
@Override
public void configure() {
super.configure();
bind(TestEndpoint.class).in(Singleton.class);
}
}
我的终点:
@Path("/test")
public class TestEndpoint {
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String get() {
return "Hello World";
}
@POST
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String post(RequestData value) {
return "Hello " + value.getName();
}
}
POJO:
@Data
@NoArgsConstructor
public class RequestData {
private String name;
}
最后 pom.xml:
<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>hu.edudroid.matek</groupId>
<artifactId>core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>core</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.8.47</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.5.v20170502</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.4.5.v20170502</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-guice</artifactId>
<version>3.1.3.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>hu.edudroid.matek.web.main.MatekServer</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
当您使用 jar-with-dependencies 程序集描述符时,所有依赖项 jar 都会提取到一个位置。有时这个罐子可以包含同名文件。在这种情况下,结果 jar 中只会包含 fist jar 中的文件。 在您的程序集文件中,META-INF\services\javax.ws.rs.ext.Providers 存在于两个依赖 jar 中(resteasy-jaxrs 和 resteasy-jettison-provider)。最后一个 jar 包含 application/json 内容类型的提供程序,但没有服务描述,resteasy 无法找到它。你可以试试 shade plugin with transformer which concatenate service description files
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>