Jersey - Maven - 找不到媒体类型= application / json的MessageBodyWriter

时间:2017-06-14 19:03:37

标签: json web-services jersey jersey-2.0 jersey-client

目前我遇到的问题是Netbeans我的webservice中的所有内容都能正常工作,但是如果我使用命令" java -jar FILENAME PARAMETERS启动jar文件会出现以下错误。

找不到媒体类型= application / json,类型的MessageBodyWriter = class java.util.ArrayList,genericType = java.util.List

我需要专家来解决这个问题:/。这很奇怪,因为当我在Netbeans中执行jar时,它可以工作。

的pom.xml

http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>at.schneider.development</groupId>
<artifactId>PhotoBoothImageService</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>PhotoBoothImageService</name>

<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.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-http</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>net.coobird</groupId>
        <artifactId>thumbnailator</artifactId>
        <version>[0.4, 0.5)</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </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>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                      <manifest>
                            <mainClass>at.schneider.development.photoboothimageservice.Main</mainClass>
                      </manifest>
                    </archive>
            </configuration>
            <executions>
              <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
              </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<properties>
    <jersey.version>2.26-b02</jersey.version>
    <jdk.version>1.8</jdk.version>
</properties>

功能:

@GET
@Path("/getimages")
@Produces(MediaType.APPLICATION_JSON)
public List<Image> getImages() {
    availableImages = getImageListFromDirectory(Paths.get(configuration.getProperties().get(BASE_DIR).toString()));
    return availableImages;
}

感谢您的建议!

最好的问候

2 个答案:

答案 0 :(得分:1)

我在commentSupercop89中找到了答案:

  

jersey-media-json-jackson必须是第一个依赖项。

答案 1 :(得分:0)

由于maven-assembly-plugin挤压了META-INF / services文件,因此JacksonAutoDiscoverable SPI似乎已消失。

通过将依赖项jersey-media-json-jackson放在顶部,它最终会处于更好的状态。

这里是一个差异:

JSON功能不起作用

META-INF/services/org.glassfish.jersey.logging.LoggingFeatureAutoDiscoverable

org.glassfish.jersey.logging.LoggingFeatureAutoDiscoverable

JSON功能工作

META-INF/services/org.glassfish.jersey.logging.LoggingFeatureAutoDiscoverable

org.glassfish.jersey.jackson.internal.JacksonAutoDiscoverable

因此,将依赖项放在首位将使Json功能正常工作,但会以您(也许)不需要的其他某些东西为代价。

有一些方法可以在maven-assembly-plugin中合并服务:Merging META-INF/services files with Maven Assembly plugin

另请参阅:https://maven.apache.org/plugins/maven-assembly-plugin/examples/single/using-container-descriptor-handlers.html

编辑:如链接中所述,我在maven-assembly-plugin上做了很多工作,但无法实现。最终,我切换到maven-shade-plugin,并且在我第一次尝试时就成功了。它还显示有关其功能的信息:

[INFO] Including org.glassfish.jersey.media:jersey-media-json-jackson:jar:2.28 in the shaded jar.

如果maven-assembly-plugin这样做了,我将无需搜索1小时。另外,它不需要一堆xml。个人结论:使用后者要容易得多。