以JSON为参数的非托管扩展 [英] Unmanaged extensions taking JSON as parameter

查看:103
本文介绍了以JSON为参数的非托管扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建扩展以动态地将一些数据插入neo4j中.

I'm trying to build an extension to dynamically insert some data in neo4j to no avail.

这是我的扩展代码:

@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对象:

And this is my Person object :

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错误,提示不支持的媒体类型"

I tried a lot of different methods but none worked so far. With the code above a get a 415 error saying "Unsupported Media Type"

我试图将jackson json提供程序添加到我的项目中.它改变了我的错误,但我不知道如何处理.现在的错误是:

I tried to add the jackson json provider to my project. It changed my error but I cannot figure out how to handle this. The error is now :

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

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

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,而Request主体如下:

To test my extension I'm using the Google REST console, setting the RequestUri as localhost:7474/myextensions/inserter/insert, the Content-Type as application/json and the Request body is the following :

{"id":100,"name":"Bibi","email":"yopmail"}

{"id":100,"name":"Bibi","email":"yopmail"}

这是Maven依赖树(不确定由于引起问题而更改了很多代码的原因,这完全引起了我的麻烦)

EDIT : Here is the maven dependency tree (not sure it was exactly the same that caused me trouble because I changed lot of code since my question)

[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

推荐答案

错误不支持的媒体类型"是由于您的方法和ajax调用中存在某些不匹配.在这里,我可以看到您的方法是发布方法,因此您必须以发布格式调用此方法.

The error "Unsupported Media Type" is due to there is some mismatch in your methos and in your ajax call. Here I can see your method is post, so you have to call this method by post format.

看看是否有效.

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

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) {}
});

这篇关于以JSON为参数的非托管扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆