4

I'm building a Neo4j (2.2.2) unmanaged extension and I can't figure out why the following method is throwing 415: unsupported media type when I post requests with properly-formatted JSON bodies and Content-Type:application/json headers.

@POST
@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
public Response createFoo(FooBar input) {
      //not relevant        
}

This is the FooBar class I want the JSON to be mapped to:

@XmlRootElement
public class FooBar implements Serializable {
    String title;    
    public FooBar(){}
}

This is the message in my log:

`SEVERE: A message body reader for Java class org.mycompany.myproject.FooBar, and Java type class     
org.mycompany.myproject.FooBar, and MIME media type application/json was not found.
The registered message body readers compatible with the MIME media type are:
** ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider      
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
....

After looking at other SO answers to similar questions, I have included the appropriate version of jersey-json and confirmed that it is being included in my jar.

This is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.mycompany</groupId>
<artifactId>myproject</artifactId>
<version>1.0</version>

<dependencies>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j</artifactId>
        <version>2.2.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.neo4j.test</groupId>
        <artifactId>neo4j-harness</artifactId>
        <version>2.2.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-kernel</artifactId>
        <version>2.2.2</version>
        <type>test-jar</type>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-io</artifactId>
        <version>2.2.2</version>
        <type>test-jar</type>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.neo4j.app</groupId>
        <artifactId>neo4j-server</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.neo4j.app</groupId>
        <artifactId>neo4j-server</artifactId>
        <version>2.2.2</version>
        <type>test-jar</type>
    </dependency>
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>server-api</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.13</version>
    </dependency>
    <dependency>
        <groupId>com.github.javafaker</groupId>
        <artifactId>javafaker</artifactId>
        <version>0.5</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.13</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.18.1</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>org.mycompany.myproject.mymainclassg</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>


    </plugins>
</build>

Note: Neo4j 2.2.2 runs Jersey 1.18.1

Update: At @StefanArmbruster's suggestion, I attempted adding jersey-media-json-jackson to the build, both with and without the jersey-json dependency, and am still having the same problem

drew moore
  • 31,565
  • 17
  • 75
  • 112
  • Can you put together a compilable project on github, so we can try it ourselves? – Michael Hunger Jun 19 '15 at 22:22
  • @MichaelHunger - I fixed it for now by including jackson-jaxrs-providers directly, but yes I'll do that when I get a chance as I'd like to figure out what was going on here – drew moore Jun 25 '15 at 22:13

3 Answers3

3

Instead of messing with Jersey's bundling, it is often simpler to simply directly include Jackson JAX-RS provider:

https://github.com/FasterXML/jackson-jaxrs-providers

(for JSON, but also for XML, Smile or CBOR)

Simply including the provider jar via Maven dependency should be enough (jar has META-INF/services auto-registration metadata); it does depend on Jackson 2.x (jackson-databind), which you may want to add an explicit dependency as well.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • @StaxMan, I'm using neo4j 3.0.3,, im also getting Unsupported MediaType like OP... So I followed the above by pasting it into my pom.xml,, but in the logs, it says: Caused by: java.lang.VerifyError: class com.fasterxml.jackson.databind.ext.CoreXMLDeserializers$Std overrides final method deserialize.(Lcom/fasterxml/jackson/core/JsonParser;Lcom/fasterxml/jackson/databind/DeserializationContext;)Ljava/lang/Object; what might go wrong? Thanks – vvavepacket Aug 28 '16 at 22:24
  • 1
    @WantIt that's a sign of incompatible (minor) versions... however, since both classes come from `jackson-databind`, perhaps it's actually problem of having `jackson-databind` jar twice in classpath somehow? – StaxMan Aug 29 '16 at 16:49
2

I've made some good experience by adding jersey-media-json-jackson to the project's build.

Also be sure to either deploy a jar-with-dependencies, or install the dependencies separately into Neo4j's plugins folder.

Stefan Armbruster
  • 39,465
  • 6
  • 87
  • 97
  • Thanks for taking a shot at it, but I tried adding jersey-media-json-jackson to the build (both with and without the jersey-json dependency) and am still throwing 415. Also, I am deploying jar-with-dependencies, and have confirmed that one of my other dependencies (java-faker) is being loaded as expected in the extension... any other ideas? This is a blocker for me... (see edited post for updated pom.xml) – drew moore Jun 19 '15 at 18:16
0

I had the same problem and fixed by including jersey-media-json-jackson into the extension JAR. Check my pom.xml (Unnecessary parts are ignored):

<dependencies>
    <!-- neo4j dependencies here -->

    <!-- javax.ws.rs dependency -->

    <!-- junit dependency -->

    <!-- JSON provider -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.22.1</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <!-- Compiler plugin -->
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>

        <!-- Maven Shade Plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <artifactSet>
                            <!-- Exlude all the dependencies except jersey-media-json-jackson -->
                            <excludes>
                                <exclude>org.neo4j</exclude>
                                <exclude>org.neo4j.*</exclude>
                                <exclude>javax.ws.rs-api</exclude>
                                <exclude>junit</exclude>
                            </excludes>
                        </artifactSet>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

The Maven Shade plugin is used to include the JSON provider into the JAR file.

Gobinath
  • 904
  • 1
  • 15
  • 23