以JSON为参数的非托管扩展

时间:2013-07-17 06:41:37

标签: java json neo4j

我正在尝试构建一个扩展,以便在neo4j中动态插入一些数据无济于事。

这是我的扩展程序代码:

@Path("/inserter")
public class Inserter {

    private final GraphDatabaseService db;

    public Inserter(@Context GraphDatabaseService db){
        this.db = db;
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Path("/insert")
    public void insert(Person x){   

        Transaction tr = db.beginTx();

        //Do something

        tr.success();
        tr.finish();
    }
}

这是我的Person对象:

package org.neo4j.server.plugin.plugin.messages;

public class Person {
    public String name;
    public int id;
    public String email;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

我尝试了很多不同的方法,但迄今为止都没有。使用上面的代码得到415错误,说“不支持的媒体类型”

我尝试将jackson json提供程序添加到我的项目中。它改变了我的错误,但我无法弄清楚如何处理这个问题。错误现在是:

  

错误500 com.fasterxml.jackson.databind.ObjectWriter.getFactory()Lcom / fasterxml / jackson / core / JsonFactory;

     

java.lang.NoSuchMethodError:com.fasterxml.jackson.databind.ObjectWriter.getFactory()Lcom / fasterxml / jackson / core / JsonFactory;

要测试我的扩展程序,我正在使用Google REST控制台,将RequestUri设置为localhost:7474 / myextensions / inserter / insert,Content-Type作为application / json,请求正文如下:

  

{ “ID”:100, “姓名”: “比比”, “电子邮件”: “yopmail”}

编辑:这是maven依赖树(不确定它是否完全相同导致我麻烦,因为我改变了很多代码,因为我的问题)

[INFO] org.neo4j.server.plugin:neo4j-antvoice-plugin:jar:0.0.1-SNAPSHOT
[INFO] +- org.apache.cxf:cxf-rt-frontend-jaxws:jar:2.7.5:compile
[INFO] |  +- xml-resolver:xml-resolver:jar:1.2:compile
[INFO] |  +- asm:asm:jar:3.3.1:compile
[INFO] |  +- org.apache.cxf:cxf-api:jar:2.7.5:compile
[INFO] |  |  +- org.codehaus.woodstox:woodstox-core-asl:jar:4.2.0:compile
[INFO] |  |  |  \- org.codehaus.woodstox:stax2-api:jar:3.1.1:compile
[INFO] |  |  +- org.apache.ws.xmlschema:xmlschema-core:jar:2.0.3:compile
[INFO] |  |  +- org.apache.geronimo.specs:geronimo-javamail_1.4_spec:jar:1.7.1:compile
[INFO] |  |  \- wsdl4j:wsdl4j:jar:1.6.3:compile
[INFO] |  +- org.apache.cxf:cxf-rt-core:jar:2.7.5:compile
[INFO] |  |  \- com.sun.xml.bind:jaxb-impl:jar:2.2.6:compile
[INFO] |  +- org.apache.cxf:cxf-rt-bindings-soap:jar:2.7.5:compile
[INFO] |  |  \- org.apache.cxf:cxf-rt-databinding-jaxb:jar:2.7.5:compile
[INFO] |  +- org.apache.cxf:cxf-rt-bindings-xml:jar:2.7.5:compile
[INFO] |  +- org.apache.cxf:cxf-rt-frontend-simple:jar:2.7.5:compile
[INFO] |  \- org.apache.cxf:cxf-rt-ws-addr:jar:2.7.5:compile
[INFO] |     \- org.apache.cxf:cxf-rt-ws-policy:jar:2.7.5:compile
[INFO] |        \- org.apache.neethi:neethi:jar:3.0.2:compile
[INFO] +- org.apache.cxf:cxf-rt-transports-http:jar:2.7.5:compile
[INFO] +- org.neo4j:neo4j:jar:2.0.0-M03:compile
[INFO] |  +- org.neo4j:neo4j-kernel:jar:2.0.0-M03:compile
[INFO] |  |  \- org.apache.geronimo.specs:geronimo-jta_1.1_spec:jar:1.1.1:compile
[INFO] |  +- org.neo4j:neo4j-lucene-index:jar:2.0.0-M03:compile
[INFO] |  |  \- org.apache.lucene:lucene-core:jar:3.6.2:compile
[INFO] |  +- org.neo4j:neo4j-graph-algo:jar:2.0.0-M03:compile
[INFO] |  +- org.neo4j:neo4j-udc:jar:2.0.0-M03:compile
[INFO] |  +- org.neo4j:neo4j-graph-matching:jar:2.0.0-M03:compile
[INFO] |  +- org.neo4j:neo4j-cypher:jar:2.0.0-M03:compile
[INFO] |  |  +- org.scala-lang:scala-library:jar:2.10.0:compile
[INFO] |  |  \- com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:jar:1.3.1:compile
[INFO] |  \- org.neo4j:neo4j-jmx:jar:2.0.0-M03:compile
[INFO] +- org.neo4j:server-api:jar:2.0.0-M03:compile
[INFO] |  +- org.neo4j.3rdparty.javax.ws.rs:jsr311-api:jar:1.1.2.r612:compile
[INFO] |  +- commons-configuration:commons-configuration:jar:1.6:compile
[INFO] |  |  +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] |  |  +- commons-lang:commons-lang:jar:2.4:compile
[INFO] |  |  +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  |  \- commons-beanutils:commons-beanutils-core:jar:1.8.0:compile
[INFO] |  \- commons-digester:commons-digester:jar:1.8.1:compile
[INFO] |     \- commons-beanutils:commons-beanutils:jar:1.8.0:compile
[INFO] +- com.google.protobuf:protobuf-java:jar:2.5.0:compile
[INFO] \- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.2.1:compile
[INFO]    +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:jar:2.2.1:compile
[INFO]    +- com.fasterxml.jackson.core:jackson-core:jar:2.2.1:compile
[INFO]    +- com.fasterxml.jackson.core:jackson-databind:jar:2.2.1:compile
[INFO]    |  \- com.fasterxml.jackson.core:jackson-annotations:jar:2.2.1:compile
[INFO]    \- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.2.1:compile

1 个答案:

答案 0 :(得分:0)

错误“不支持的媒体类型”是由于您的方法和您的ajax调用中存在一些不匹配。在这里,我可以看到你的方法是post,所以你必须通过post格式调用这个方法。

看看它是否有效。

json_array * = [{“id”:100,“name”:“Bibi”,“email”:“yopmail”}]

$.ajax({
type:'POST',
url :'http://localhost:8080/inserter/insert',
cache: false,
contentType: "application/json",
dataType: 'json',
data:json_array,
success:function(data){},
error:function(response,status) {}
});