2

I'm getting this error only when running from the fatjar, mvn run:java works as expected.

SEVERE: MessageBodyWriter not found for media type=application/json, type=class com.example.Greeting, genericType=class com.example.Greeting.

I'm using moxy for json handling. I tried jackson, made no difference. Tried grizzly instead o jetty, same issue.

Here are my pom dependencies:

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-http</artifactId>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.test-framework</groupId>
        <artifactId>jersey-test-framework-core</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.test-framework.providers</groupId>
        <artifactId>jersey-test-framework-provider-jetty</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
    </dependency>
</dependencies>

Shade plugin config:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.3</version>
    <configuration>
        <createDependencyReducedPom>true</createDependencyReducedPom>
        <filters>
            <filter>
                <artifact>*:*</artifact>
                <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                </excludes>
            </filter>
        </filters>
    </configuration>
    <executions>
        <execution>
            <id>shade</id>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <!--<minimizeJar>true</minimizeJar>-->
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.example.http.JettyHttpServer</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

Jersey version is 2.30.1.

Thanks!

EDIT

Contents of org.glassfish.jersey.internal.spi.AutoDiscoverable does not include org.glassfish.jersey.moxy.json.internal.MoxyJsonAutoDiscoverable

org.glassfish.jersey.logging.LoggingFeatureAutoDiscoverable
org.glassfish.jersey.internal.config.ExternalPropertiesAutoDiscoverable

According to this it should. How do I fix it?

lsborg
  • 1,191
  • 1
  • 12
  • 17

1 Answers1

3

Try loading the dependencies you need explicitly on code and not relying on auto discovery, registering them on Jersey.

The fat jar plug-in tries to analyze your code and see what's being used and what's not, so it can exclude references not used and make a smaller jar. The problem with auto discovery is that since there are no explicit references to some classes, the shade plug-in can just think their aren't being used so they can be removed.

I think there's a way to force them to be included on the plug-in configuration, but I don't remember that configuration from memory, you would have to search for it in the docs.

Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
  • 1
    It worked after registering MoxyJsonFeature `resourceConfig.register(MoxyJsonFeature.class)`, thanks! I tried to find a way to aggregate all AutoDiscoverable but failed. – lsborg May 25 '20 at 18:59
  • 1
    Thanks. Similarly in my case adding the following made it work: `client.register(new JacksonFeature());` – Ati Ranzuglia Jun 15 '22 at 04:16