简体   繁体   English

具有泽西依赖关系的Gradle jar会为media type = application / json找不到MessageBodyReader

[英]Gradle jar with jersey dependencies produces MessageBodyReader not found for media type=application/json

I've written a Jersey client which communicates with a 3rd party service and it works if I run it from Eclipse but it throws a MessageBodyProviderNotFoundException if I run the jar file. 我编写了一个与第三方服务进行通信的Jersey客户端, 如果我从Eclipse运行它它会起作用,但如果我运行jar文件,它会抛出MessageBodyProviderNotFoundException

My build.gradle: 我的build.gradle:

// Apply the java plugin to add support for Java
apply plugin: 'java'

version = '1.0'

// In this section you declare where to find the dependencies of your project
repositories {
    jcenter()
}

//create a single Jar with all dependencies
task createJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': 'MeteorDesk Whatsapp Images Parser',  
            'Implementation-Version': version,
            'Main-Class': 'controller.ImagesParser'
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

// In this section you declare the dependencies for your production and test code
dependencies {
    compile 'org.slf4j:slf4j-api:1.7.12'

    compile 'org.glassfish.jersey.core:jersey-client:2.22.1'
    compile 'org.glassfish.jersey.containers:jersey-container-servlet-core:2.22.1'
    compile 'org.glassfish.jersey.media:jersey-media-json-jackson:2.22.1'
    compile 'com.google.guava:guava:19.0'
    compile 'joda-time:joda-time:2.4'
}

Here goes the exception: 这是一个例外:

 org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json, type=class model.login.LoginResult, genericType=class model.login.LoginResult.
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:231)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1085)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:808)
    at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:326)
    at org.glassfish.jersey.client.InboundJaxrsResponse$1.call(InboundJaxrsResponse.java:115)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:419)
    at org.glassfish.jersey.client.InboundJaxrsResponse.runInScopeIfPossible(InboundJaxrsResponse.java:267)
    at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:112)
    at controller.MeteorDeskService.login(MeteorDeskService.java:42)

I read this post from someone who had the exact same problem and his solution was to use Maven instead, so I tried also with Maven, but I got the same exception. 我从一个有完全相同问题的人那里读到这篇文章 ,而他的解决方案是使用Maven,所以我也尝试使用Maven,但我得到了同样的例外。

I can also see MessageBodyReader.class inside the generated jar. 我还可以在生成的jar中看到MessageBodyReader.class。

Does anyone have any clue why is this happening? 有没有人知道为什么会发生这种情况?

As @peeskillet suggested, using the Maven Shade Plugin did the trick, this is what I added to my pom.xml: 正如@peeskillet建议的那样,使用Maven Shade插件就可以了,这就是我添加到我的pom.xml中的内容:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <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>your main class here</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

For someone looking for solution in gradle itself, use Shadow plugin . 对于在gradle中寻找解决方案的人,请使用Shadow插件 Add following lines in your build.gradle. 在build.gradle中添加以下行。

buildscript {
        repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "com.github.jengelman.gradle.plugins:shadow:1.2.4"
    }
}
apply plugin: "com.github.johnrengelman.shadow"

shadowJar {
    baseName = '__Title__'
    mergeServiceFiles()    //For Merging Service Descriptor Files
}

+1 on the beer to @peeskillet. +1啤酒到@peeskillet。 Had the same issue with maven build of JMH performance benchmark that builds shaded jar. 与构建阴影jar的JMH性能基准的maven构建存在相同的问题。 Here is the relevant part of pom.xml: 这是pom.xml的相关部分:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <finalName>${uberjar.name}</finalName>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>org.openjdk.jmh.Main</mainClass>
                            </transformer>
                        </transformers>
                        <filters>
                            <filter>
                                <!-- Shading signed JARs will fail without this. http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar -->
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 泽西错误:找不到媒体类型= application / json的MessageBodyReader - Jersey Error: MessageBodyReader not found for media type=application/json 找不到 Media type=application/json 的 MessageBodyReader - MessageBodyReader not found for media type=application/json 找不到针对媒体类型= application / json的MessageBodyReader-android - MessageBodyReader not found for media type=application/json - android Jersey + Json 媒体类型 application/json 未找到 - Jersey + Json media type application/json was not found Jersey:找不到媒体类型= multipart / form-data的MessageBodyReader - Jersey: MessageBodyReader not found for media type=multipart/form-data 找不到媒体类型= application / octet-stream的MessageBodyReader - MessageBodyReader not found for media type=application/octet-stream 泽西与Grizzly:找不到媒体类型= application / json的MessageBodyWriter - Jersey with Grizzly: Getting MessageBodyWriter not found for media type=application/json 泽西(Jersey)REST错误,找不到媒体类型= application / json的MessageBodyWriter - Jersey REST error, MessageBodyWriter not found for media type=application/json 泽西岛无法封送数据-找不到MIME媒体类型application / json - Jersey unable to Marshall the data - MIME media type application/json was not found 获取MessageBodyProviderNotFoundException:找不到媒体类型= text / plain的MessageBodyReader当输出为JSON字符串时 - Getting MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/plain When output is JSON string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM