【问题标题】:Consuming JSON in neo4j server extension在 neo4j 服务器扩展中使用 JSON
【发布时间】:2015-06-19 02:57:11
【问题描述】:

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

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

在查看了类似问题的其他 SO 答案后,我已包含适当版本​​的 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 依赖项,但我仍然遇到同样的问题

【问题讨论】:

  • 能否在github上整理一个可编译的项目,我们自己试试?
  • @MichaelHunger - 我现在通过直接包含 jackson-jaxrs-providers 来修复它,但是是的,当我有机会时我会这样做,因为我想弄清楚这里发生了什么

标签: java neo4j jersey jackson jax-rs


【解决方案1】:

与其搞乱 Jersey 的捆绑,直接包含 Jackson JAX-RS 提供程序通常更简单:

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

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

只需通过 Maven 依赖包含提供程序 jar 就足够了(jar 具有 META-INF/services 自动注册元数据);它确实依赖于 Jackson 2.x (jackson-databind),您可能还需要添加显式依赖项。

【讨论】:

  • @StaxMan,我正在使用 neo4j 3.0.3,我也得到了 Unsupported MediaType like OP... 所以我按照上面的方法将它粘贴到我的 pom.xml 中,但是在日志中,它说: 引起:java.lang.VerifyError:com.fasterxml.jackson.databind.ext.CoreXMLDeserializers$Std 覆盖最终方法反序列化。(Lcom/fasterxml/jackson/core/JsonParser;Lcom/fasterxml/jackson/databind /DeserializationContext;)Ljava/lang/Object;可能会出什么问题?谢谢
  • @WantIt 这是不兼容(次要)版本的标志......但是,由于这两个类都来自jackson-databind,也许它实际上是在类路径中有两次jackson-databind jar 的问题?跨度>
【解决方案2】:

通过将jersey-media-json-jackson 添加到项目的构建中,我获得了一些很好的经验。

同时确保部署一个 jar-with-dependencies,或者将依赖项单独安装到 Neo4j 的 plugins 文件夹中。

【讨论】:

  • 感谢您试一试,但我尝试将 jersey-media-json-jackson 添加到构建中(有和没有 jersey-json 依赖项)并且仍然抛出 415。另外,我我正在部署 jar-with-dependencies,并已确认我的其他依赖项之一(java-faker)正在按预期在扩展中加载......还有其他想法吗?这对我来说是一个障碍......(请参阅已编辑的帖子以获取更新的 pom.xml)
【解决方案3】:

我遇到了同样的问题,并通过在扩展 JAR 中包含 jersey-media-json-jackson 来解决。检查我的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 文件中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2012-04-11
    相关资源
    最近更新 更多