java - 未找到媒体类型 = application/json 的 JAX RS MessageBodyWriter

标签 java rest jax-rs

我正在使用 JAX RS Jersey 2 创建 Rest 客户端。该客户端可以工作,但只能在我的 IDE (IntellIJ IDEA) 中工作,当我使用 Maven、使用 maven-assemble-plugin 构建它并运行 jar 时,它不再工作.

我收到 MessageBodyWriter not found for media type=application/json 错误。

我尝试添加人们在其他帖子中建议的更多依赖项,但我认为依赖项不是问题,因为它在 IDE 中运行。

这里是抛出异常的代码

return client.target(uri)
        .request(MediaType.APPLICATION_JSON)
        .post(Entity.entity(transactions.get(0), MediaType.APPLICATION_JSON));

调试后,当我用空字符串“”替换 transactions.get(0) 时,它可以工作。

这是 maven 的 pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>SequencerControllers</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>com.sparkjava</groupId>
        <artifactId>spark-core</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.25.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.25.1</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.21</version>
    </dependency>
</dependencies>

我错过了什么吗?确实让我困扰的是它在 IDE 中运行,但在使用 Maven 构建时却无法运行,因为我使用依赖项构建它。

最佳答案

检查生成的 JAR 中是否有类 com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider 。如果没有,请添加以下依赖项:

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>2.10.0</version>
</dependency>

关于java - 未找到媒体类型 = application/json 的 JAX RS MessageBodyWriter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58511103/

相关文章:

java - MongoDB:合并两个 .find() 语句

java - 获取 Amazon Kinesis 异常状态代码 : 502 when using LocalStack from Java

java - 需要 Solaris 和 Java 连续内存说明

java - Jersey Client 2.19 不会因错误状态代码而引发异常

java - 如何删除 nullPointerException

java - 在 Restful 服务中获取 HttpServletResponse Null

php - 是否可以使用 Azure 通信仅使用 REST 端点发送 SMS?

java - 谁初始化@Post(或任何 jax rs 注释)执行的方法中的参数?

glassfish - 强制 glassfish 4 使用 Jackson 2.3

java - JAX-RS 仅运行一个函数一次