java - MessageBodyProviderNotFoundException while running jar from command line -
i using java jersey framework(with maven), , use intellij ide. have encountered runtime exception happens when try run code command line (using maven compile , java -jar ) not when running within intellij, strange.
i have java code try make http on remote url , try read returned json lombok pojo :
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 not sure why, when hits last line "readentity()" method, error below:
org.glassfish.jersey.message.internal.messagebodyprovidernotfoundexception: messagebodyreader not found media type=text/json; charset=utf-8
this strange, because have jersey-media-json-jackson dependency specified in pom.xml :
<dependency> <groupid>org.glassfish.jersey.media</groupid> <artifactid>jersey-media-json-jackson</artifactid> <version>2.23</version> </dependency>
and pojo class trying readentity() :
@data @jsonignoreproperties(ignoreunknown = true) public class parseresponse { @jsonproperty("id") private integer id; ...other params... }
and mentioned before, strange happens when try run on command line there no error when running in intellij:
mvn clean package java -jar target/nameofjar.jar
did miss obvious here? have looked @ other people similar issues online haven't found solution.
thanks is
if inside jersey-media-json-jackson
jar should see file
meta-inf/services/org.glassfish.jersey.internal.spi.autodiscoverable
the contents of file should single qualified name of class implements name of file, namely
org.glassfish.jersey.jackson.internal.jacksonautodiscoverable
this file used jersey auto-discoverable mechanism automatically register features without having explicitly register them. briefly, how works, jersey modules/jars have components should automatically registered, should have above named file located in jar, contents being name(s) of auto-discoverable component. jersey use service loader pattern load classes named in file, , register them.
the problem causes when creating uber jars can have 1 copy of file, can't have duplicates. if have multiple jars above file? 1 of files included in uber jar. one? knows, there 1 lucky winner. rest of jars, auto-discover mechanism never kicks in. case jackson feature, auto-discoverable registers jacksonfeature
. can try explicitly register application, , should see works.
but other jars/modules may have file? it's reason when creating uber jars, should use maven-shade-plugin. plugin allows do, combine contents of files all discoverables included 1 single file. below example usage
<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>
this example taken dropwizard's getting started. can check out further explanation. main part of concern servicesresorcetransformer
, concatenates services files.
Comments
Post a Comment