我正在使用Java Jersey框架(与Maven),并使用IntelliJ作为我的IDE。我遇到了这个运行时异常,仅当我尝试从命令行运行代码(使用maven进行编译,然后使用java -jar)时才发生,而在IntelliJ中运行时却没有发生。
我有一些Java代码,这些代码将尝试在某个远程URL上进行HTTP GET,并尝试将返回的JSON读入Lombok POJO中:
String targetUrl = "some valid URL";
WebTarget webTarget = client.target(targetUrl);
Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).get();
ParseResponse parseResponse = response.readEntity(ParseResponse.class);
我不确定为什么,但是当它碰到执行“ readEntity()”方法的最后一行时,我将得到以下错误:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/json; charset=utf-8
这很奇怪,因为我肯定在pom.xml中指定了jersey-media-json-jackson依赖项:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.23</version>
</dependency>
这是我试图将Entity()读入的POJO类:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ParseResponse {
@JsonProperty("id")
private Integer id;
...Other params...
}
就像我之前提到的,奇怪的是,仅当我尝试在这样的命令行上运行时才发生这种情况,但是在IntelliJ中运行时没有错误:
mvn clean package
java -jar target/NameOfJar.jar
我想念这里明显的东西吗?我在网上看过其他有类似问题的人,但还没有找到解决方案。
谢谢IS
如果您在jersey-media-json-jackson
罐子里看,您会看到一个文件
META-INF/services/org.glassfish.jersey.internal.spi.AutoDiscoverable
该文件的内容应该是实现文件名的类的单个完全限定名称,即
org.glassfish.jersey.jackson.internal.JacksonAutoDiscoverable
Jersey 自动发现机制使用此文件来自动注册功能,而无需我们显式注册它们。简而言之,它的工作原理是所有具有应自动注册的组件的Jersey模块/ jar都应在jar中包含上述命名的文件,其内容为可自动发现的组件的名称。然后,Jersey将使用Service Loader模式加载文件中命名的类,并进行注册。
创建uber jar时,这导致的问题是您只能拥有一个文件的副本,而不能拥有副本。那如果上面的文件有多个jar呢?那么这些文件中只有一个会被包含在uber jar中。哪一个?谁知道,但只有一位幸运的获胜者。因此,对于其余的jars,它们的自动发现机制永远不会起作用。Jackson功能就是这种情况,在该功能中,自动发现功能会注册JacksonFeature
。您可以尝试向您的应用程序显式注册,并且应该看到它现在可以工作了。
但是其他可能有此文件的jar /模块呢?因此,在创建uber jar时,应使用maven-shade-plugin。该插件允许您执行的操作是合并文件的内容,以便所有可发现内容都包含在该单个文件中。以下是用法示例
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.YourApp</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
该示例实际上取自Dropwizard的《入门指南》。您可以查看进一步的解释。关注的主要部分是ServicesResorceTransformer
,它是服务文件的连接。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句