Unsupported media type when using uberjar jersey and jackson

I am currently facing a problem with Jersey and Jackson where I cannot find a solution: when I try to get a POJO serialized from JSON as a POST parameter at the Jersey endpoint, it returns an error if I call it from ubergar. If I use the same wget-call after I started the main method from eclipse, everything works fine, and I get the expected answers. I looked for other people having problems using types of knitted and postparametric applications, such as Error 415 Unsupported media type: POST does not reach REST if JSON, but this happens if XML and JAX-RS: how to automatically serialize a collection when returning a Response object ? , but the main errors in which they did not indicate headers in the call or use the wrong @ Receptions-tags that I donโ€™t have. According to my research, no one had similar problems that the call worked when the server started from eclipse, but did not work when the server was started from the outside.

The class that starts the server is as follows:

public class ServerStarter { public static final String BASE_URI = "http://localhost:8282/test/"; public static void main(String[] args) throws IOException { final ResourceConfig rc = new ResourceConfig().packages("starter"); HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc); System.out.println(String.format("Drรผcke Enter um Server zu beenden.", BASE_URI)); System.in.read(); server.stop(); } } 

And the endpoint is as follows:

 @Path("testme") public class Endpoint { @POST @Consumes(MediaType.APPLICATION_JSON) public String testMe(Parameters ep) { return ep.toString(); } } 

And the Parameters class, which should be a POJO, looks like this:

 public class Parameters { private int x; public int getX() { return x; } public void setX(int x) { this.x = x; } } 

If I do mvn clean package assembly: single, start the server and then call wget localhost:8282/test/testme --post-data='{"x": 10 }' --header="Content-Type: application/json" , it will return 415 Unsupported Media Type .

The dependencies in pom.xml are as follows:

  <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-grizzly2-http</artifactId> <version>2.13</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.13</version> </dependency> 

Does anyone know how to solve this problem? I do not know, as far as I thought, a call from an eclipse should look like a call in uberjar.

+7
source share
2 answers

This problem should have been caused by the services ( http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html#Service_Provider ) that are provided by banks. While eclipse combines them correctly, uberjar-plugins force them to overwrite each other. One possible solution is to use the maven-assembly-plugin instead, using the following assembly descriptor:

 <assembly> <id>production</id> <formats> <format>dir</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <dependencySets> <dependencySet> <useProjectArtifact>false</useProjectArtifact> <outputDirectory>lib</outputDirectory> <unpack>false</unpack> </dependencySet> </dependencySets> <fileSets> <fileSet> <directory>${project.build.directory}/classes</directory> <outputDirectory>classes</outputDirectory> </fileSet> </fileSets> </assembly> 

You can then start the application using java -cp lib/:classes/ MainClass .

0
source

David analysis is correct. I just want to add an alternative solution. You can use the Maven Shade plugin with ServicesResourceTransformer , for example, to

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.1</version> <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> <transformers> <!-- To ensure the original META-INF/services definitions are respected --> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>id.web.michsan.hellotransfer.Application</mainClass> </transformer> </transformers> <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> </execution> </executions> </plugin> 
0
source

Source: https://habr.com/ru/post/1203732/


All Articles