gpt4 book ai didi

java - 使用 JSON 作为参数的非托管扩展

转载 作者:行者123 更新时间:2023-12-02 15:51:14 25 4
gpt4 key购买 nike

我正在尝试构建一个扩展来在 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 提供程序添加到我的项目中。它改变了我的错误,但我不知道如何处理这个问题。现在的错误是:

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;

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

{"id":100,"name":"Bibi","email":"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

最佳答案

错误“不支持的媒体类型”是由于您的方法和 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) {}
});

关于java - 使用 JSON 作为参数的非托管扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17692869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com