【问题标题】:"JAX-RS MessageBodyWriter not found for media type=application/json" executing jar but mvn:exec works"JAX-RS MessageBodyWriter not found for media type=application/json" 执行 jar 但 mvn:exec 有效
【发布时间】:2015-11-25 16:10:21
【问题描述】:

我在尝试打包我的 Maven 项目时遇到问题。我使用 mvn:exec 开发和测试了它,一切正常。 使用以下 pom.xml 我生成了带有依赖项的 jar 文件(使用 mvn 包)。应用程序启动正常,但是当我在日志上向 JAX-RS 网络服务发送请求时,我得到了这个:

[2015-11-25 16:55:05] [org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo] SEVERE: MessageBodyWriter not found for media type=application/json, ....

对于像字符串或整数这样更简单的对象,在客户端我得到了 json 的“415 - 不支持的媒体类型”。

我已经尝试将 json-media-moxy 添加到依赖项或使用 java -cp 而不是 -jar 执行 jar,但没有任何效果。

这是我的 pom:

[...]
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-grizzly2-http</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
        [...]
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>it.project.Application</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>it.project.Application</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <properties>
        <jersey.version>2.22</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

解决了!只需添加

rc.register(JacksonFeature.class);

到我的 ResourceConfig 以使事情正常进行。我不知道为什么但是使用 mvn package 和 jar 执行 jersey 不执行自动发现,所以 jackson 没有注册。

【问题讨论】:

标签: java json web-services maven


【解决方案1】:

尝试添加这个依赖(适应你的球衣版本)

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.22</version>
</dependency>

我猜没有 jersey 所需的库来处理 maven 依赖项中的 json 数据

更新

我的错误,我刚刚看到你已经拥有它。你的要求怎么样?与自定义客户端?如果您使用的是自定义客户端,请尝试此操作

Client client = ClientBuilder.newClient().register(JacksonFeature.class);

【讨论】:

  • 不,我的客户端是 angularjs(所以是用户浏览器),但即使使用 curl 或 intellij 工具,结果也是一样的。
【解决方案2】:

解决了!只需添加

rc.register(JacksonFeature.class); 到我的 ResourceConfig 以使事情正常进行。我不知道为什么但是使用 mvn package 和 jar 执行 jersey 不执行自动发现,所以 jackson 没有注册。

【讨论】:

    猜你喜欢
    • 2014-10-17
    • 1970-01-01
    • 2014-01-12
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    相关资源
    最近更新 更多