【发布时间】: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 没有注册。
【问题讨论】:
-
对于试图了解为什么会发生这种情况的观众:stackoverflow.com/a/36687730/4178025
标签: java json web-services maven