使用嵌入式JanusGraph,我创建了一个返回Vertex和Edge列表的REST服务,但是我想像gremlin服务器一样返回json。
我试图使用MessageSerializer,但我无法将二进制格式强制转换为json。
@RequestMapping("/search")
public String search(@RequestBody SearchOptions options) throws JsonProcessingException, SerializationException {
List<Object> data = new ArrayList<>();
Builder responseBuilder = getResponseBuilder(data);
List<Vertex> nodes;
List<Object> edges;
if (Strings.isNullOrEmpty(options.getText()) || Strings.isNullOrEmpty(options.getField())) {
nodes = g.V().limit(options.getLimit()).toList();
edges = g.V().limit(options.getLimit()).aggregate("node").outE().as("edge").inV().where(P.within("node"))
.select("edge").toList();
} else {
nodes = g.V().has(options.getField(), options.getText()).toList();
edges = g.V().has(options.getField(), options.getText()).aggregate("node").outE().as("edge").inV()
.where(P.within("node")).select("edge").toList();
}
data.add(nodes);
data.add(edges);
ResponseMessage response = responseBuilder.create();
ByteBufAllocator allocator = new PooledByteBufAllocator();
ByteBuf byteBuffer = ser.serializeResponseAsBinary(response, allocator);
byte[] bytes = byteBuffer.array();
String str = new String(bytes);
return str;
}
@Bean
public MessageSerializer messageSerializer() {
GryoMapper.Builder kryo = GryoMapper.build().addRegistry(JanusGraphIoRegistry.getInstance());
MessageSerializer serializer = new GryoMessageSerializerV1d0(kryo);
return serializer;
}
我尝试使用jackson对象映射器,但得到此错误 -
[http-nio-8080-exec-10] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
org.apache.tinkerpop.shaded.jackson.databind.JsonMappingException: (was java.lang.IllegalStateException) (through reference chain: org.apache.tinkerpop.gremlin.driver.message.ResponseMessage["result"]->org.apache.tinkerpop.gremlin.driver.message.ResponseResult["data"]->java.util.ArrayList[0]->java.util.ArrayList[0]->org.janusgraph.graphdb.relations.RelationIdentifier["inVertexId"])
at org.apache.tinkerpop.shaded.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:388)
我像这样定义ObjectMapper -
ObjectMapper mapper = GraphSONMapper.build().version(GraphSONVersion.V2_0).create().createMapper();
仅供参考 - 这是创建ObjectMapper的正确方法。
private ObjectMapper mapper = GraphSONMapper.build()
.addRegistry(JanusGraphIoRegistry.getInstance())
.version(GraphSONVersion.V2_0).create().createMapper();
答案 0 :(得分:1)
我认为您应该使用您需要的任何配置从ObjectMapper
实例创建一个Jackson GraphSONMapper
,然后将结果传递给:
gremlin> mapper = GraphSONMapper.build().version(GraphSONVersion.V2_0).create().createMapper()
==>org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper@4ca8dbfa
gremlin> mapper.class
==>class org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper
gremlin> vertices = g.V().toList()
==>v[1]
==>v[2]
==>v[3]
==>v[4]
==>v[5]
==>v[6]
gremlin> mapper.writeValueAsString(vertices)
==>[{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":1},"label":"person","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":0},"value":"marko","label":"name"}}],"age":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":1},"value":{"@type":"g:Int32","@value":29},"label":"age"}}]}}},{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":2},"label":"person","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":2},"value":"vadas","label":"name"}}],"age":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":3},"value":{"@type":"g:Int32","@value":27},"label":"age"}}]}}},{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":3},"label":"software","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":4},"value":"lop","label":"name"}}],"lang":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":5},"value":"java","label":"lang"}}]}}},{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":4},"label":"person","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":6},"value":"josh","label":"name"}}],"age":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":7},"value":{"@type":"g:Int32","@value":32},"label":"age"}}]}}},{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":5},"label":"software","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":8},"value":"ripple","label":"name"}}],"lang":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":9},"value":"java","label":"lang"}}]}}},{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":6},"label":"person","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":10},"value":"peter","label":"name"}}],"age":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":11},"value":{"@type":"g:Int32","@value":35},"label":"age"}}]}}}]