MessageBodyProviderNotFoundException when running jar from command line

I use the Java Jersey framework (with Maven) and use IntelliJ as my IDE. I came across this exception, which ONLY happens when I try to run code from the command line (using maven to compile and then java -jar), but NOT when starting in IntelliJ, which is strange.

I have some Java code that will try to do an HTTP GET on some remote URL and try to read the returned JSON in some POJO Lombok:

String targetUrl = "some valid URL";

WebTarget webTarget = client.target(targetUrl);

Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).get();

ParseResponse parseResponse = response.readEntity(ParseResponse.class);

I'm not sure why, but when it gets to the last line that executes the readEntity () method, I get the following error:

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/json; charset=utf-8

This is strange because I have a definitely defined jersey-media-json-jackson dependency in my pom.xml:

<dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.23</version>
</dependency>

POJO, Entity() :

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ParseResponse {

   @JsonProperty("id")
   private Integer id;

   ...Other params...
}

, , , , , , IntelliJ:

mvn clean package
java -jar target/NameOfJar.jar

- ? , , , .

IS

+5
1

jersey-media-json-jackson,

META-INF/services/org.glassfish.jersey.internal.spi.AutoDiscoverable

, ,

org.glassfish.jersey.jackson.internal.JacksonAutoDiscoverable

Jersey . , , / , , , , , () . Service Loader pattern , , .

, uber jars, , , . , , ? , uber. ? , . . , JacksonFeature. , , .

/, ? , uber, maven-shade-plugin. , , , .

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>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>
            <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>com.example.YourApp</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

Dropwizard. . : ServicesResorceTransformer, .

+11

All Articles