在neo4j服务器扩展中使用JSON

时间:2015-06-19 02:57:12

标签: java neo4j jersey jackson jax-rs

我正在构建一个Neo4j(2.2.2)非托管扩展,当我使用格式正确的JSON主体和415: unsupported media type发布请求时,我无法弄清楚为什么以下方法会抛出Content-Type:application/json头。

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

这是我想要将JSON映射到的FooBar类:

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

这是我日志中的消息:

`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
....

在查看其他类似问题的答案之后,我已经包含了jersey-json的相应版本并确认它已包含在我的jar中。

这是我的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>

注意:Neo4j 2.2.2运行Jersey 1.18.1

更新:在@ StefanArmbruster的建议下,我尝试将jersey-media-json-jackson添加到构建中,无论是否有jersey-json依赖,我仍然遇到同样的问题

3 个答案:

答案 0 :(得分:3)

简单地直接包含杰克逊JAX-RS提供商,而不是搞乱泽西岛的捆绑销售:

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

(对于JSON,但也适用于XML,Smile或CBOR)

只需通过Maven依赖包含提供者jar就足够了(jar有META-INF / services自动注册元数据);它确实依赖于Jackson 2.x(jackson-databind),你可能也希望添加一个明确的依赖。

答案 1 :(得分:2)

通过在项目构建中添加jersey-media-json-jackson,我获得了很好的体验。

还要确保部署jar-with-dependencies,或者将依赖项分别安装到Neo4j的plugins文件夹中。

答案 2 :(得分:0)

我遇到了同样的问题,并通过将jersey-media-json-jackson包含在扩展JAR中来解决。检查我的pom.xml忽略不必要的部分):

<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>

Maven Shade插件用于将JSON提供程序包含到JAR文件中。