4

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.

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

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.

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

Does anyone have any clue why is this happening?

Community
  • 1
  • 1
muilpp
  • 1,293
  • 4
  • 17
  • 36
  • 1
    I'm guessing the META-INF/services files are overwriting each other when you create the fat jar. The META-INF/services files are how Jersey loads a lot of it's auto-registered features, Jackson included. If this is the case, which I imagine it is, you can just explicitly register the `JacksonFeature` with the `Client`. The solution to the META/services problem is the use the shade plugin when creating a fat jar. With shade, you can combine the contents of the files with the same name, so that they don't overwrite each other. I'm not sure about Gradle. I don't really use it – Paul Samsotha Mar 05 '16 at 14:07
  • 1
    The META-INF/services file I'm referring to, is the `org.glassfish.jersey.internal.spi.AutoDiscoverable` file. Different jars have this same file, and it is used to auto-load features specific to that jar. Maybe you can create a Gradle task to combine the files into one. I have no idea how to do that. Or just register the `JacksonFeature` like I mentioned. But I am not sure if there are any other features you might lose. But if you are only using the client, I would imagine it shouldn't be a problem – Paul Samsotha Mar 05 '16 at 14:13
  • @peeskillet It took me a while but finally I used the shade plugin of maven and now it works. You saved me twice the same week, I owe you a beer at least! – muilpp Mar 07 '16 at 17:30
  • Cheers! Can you post your solution as an answer to close the question, and so others can benefit. I would've posted an answer, but I wanted it to be complete with shade example, but I didn't have time. I'm sure your solution would save others time also :-) – Paul Samsotha Mar 08 '16 at 05:41

3 Answers3

5

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>
muilpp
  • 1,293
  • 4
  • 17
  • 36
3

For someone looking for solution in gradle itself, use Shadow plugin. Add following lines in your 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
}
  • Thanks, it helped me. Just in case someone else runs into same problem, I solved it by adding only shadowJar { mergeServiceFiles() } to `build.gradle`. Looks like plugin `id "com.github.johnrengelman.shadow" version "4.0.3"` does shadowing wrong way without it. – metamaker Dec 02 '18 at 22:44
-2

+1 on the beer to @peeskillet. Had the same issue with maven build of JMH performance benchmark that builds shaded jar. Here is the relevant part of 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>
gone.skiing
  • 343
  • 3
  • 2