具有球衣依赖项的Gradle jar会为媒体type = application/json找不到MessageBodyReader [英] Gradle jar with jersey dependencies produces MessageBodyReader not found for media type=application/json

查看:120
本文介绍了具有球衣依赖项的Gradle jar会为媒体type = application/json找不到MessageBodyReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个Jersey客户端,该客户端与第三方服务进行通信,并且如果我从Eclipse运行它,它可以工作,但是如果运行jar,它会抛出 MessageBodyProviderNotFoundException 文件.

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.

我的build.gradle:

My 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'
}

这里是个例外:

 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)

我从某人阅读了这篇文章有完全相同的问题,他的解决方案是改用Maven,所以我也尝试了Maven,但我遇到了同样的异常.

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.

我还可以在生成的jar中看到MessageBodyReader.class.

I can also see MessageBodyReader.class inside the generated jar.

有人知道为什么会这样吗?

Does anyone have any clue why is this happening?

推荐答案

按照@peeskillet的建议,使用 Maven Shade插件达到了目的,这就是我添加到pom.xml中的内容: /p>

As @peeskillet suggested, using the Maven Shade Plugin did the trick, this is what I added to my 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>

这篇关于具有球衣依赖项的Gradle jar会为媒体type = application/json找不到MessageBodyReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆