Created
March 11, 2011 16:24
-
-
Save peterneubauer/866123 to your computer and use it in GitHub Desktop.
Custom representation plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Depend on | |
------------------- | |
<dependency> | |
<groupId>org.neo4j.app</groupId> | |
<artifactId>neo4j-server</artifactId> | |
<version>1.3-SNAPSHOT</version> | |
</dependency> | |
----------------------- | |
package org.neo4j.examples.server.plugins; | |
import org.neo4j.graphdb.GraphDatabaseService; | |
import org.neo4j.server.plugins.Description; | |
import org.neo4j.server.plugins.Name; | |
import org.neo4j.server.plugins.PluginTarget; | |
import org.neo4j.server.plugins.ServerPlugin; | |
import org.neo4j.server.plugins.Source; | |
import org.neo4j.server.rest.repr.MyRepresentation; | |
@Description( "dempstrates how to return custom data as a representation" ) | |
public class CustomRepresentation extends ServerPlugin | |
{ | |
@Name( "custom_representation" ) | |
@Description( "Serve a custom representation of the reference node" ) | |
@PluginTarget( GraphDatabaseService.class ) | |
public MyRepresentation getAllNodes( @Source GraphDatabaseService graphDb ) | |
{ | |
return new MyRepresentation(graphDb.getReferenceNode()); | |
} | |
} | |
-------------------------- | |
package org.neo4j.server.rest.repr; | |
import org.neo4j.graphdb.Node; | |
public class MyRepresentation extends ObjectRepresentation | |
{ | |
private final Node node; | |
public MyRepresentation( Node node ) | |
{ | |
super( RepresentationType.NODE ); | |
this.node = node; | |
} | |
@Mapping("hello_world") | |
public ValueRepresentation sayHello() { | |
return ValueRepresentation.string( "hello world from node " + node.getId() ); | |
} | |
} | |
------------------------- | |
list it in META-INF/services/org.neo4j.server.plugins.ServerPlugin | |
org.neo4j.examples.server.plugins.CustomRepresentation | |
Deploy it according to http://docs.neo4j.org/chunked/snapshot/server-plugins.html | |
Result: | |
[~/code/neo/server/examples] $curl http://localhost:7474/db/data/ | |
{ | |
"relationship_index" : "http://localhost:7474/db/data/index/relationship", | |
"node" : "http://localhost:7474/db/data/node", | |
"relationship_types" : "http://localhost:7474/db/data/relationship/types", | |
"extensions_info" : "http://localhost:7474/db/data/ext", | |
"node_index" : "http://localhost:7474/db/data/index/node", | |
"reference_node" : "http://localhost:7474/db/data/node/0", | |
"extensions" : { | |
"GetAll" : { | |
"get_all_nodes" : "http://localhost:7474/db/data/ext/GetAll/graphdb/get_all_nodes", | |
"getAllRelationships" : "http://localhost:7474/db/data/ext/GetAll/graphdb/getAllRelationships" | |
}, | |
"CustomRepresentation" : { | |
"custom_representation" : "http://localhost:7474/db/data/ext/CustomRepresentation/graphdb/custom_representation" | |
} | |
} | |
}[~/code/neo/server/examples] curl http://localhost:7474/db/data/ext/CustomRepresentation/graphdb/custom_representation | |
{ | |
"extends" : "graphdb", | |
"description" : "Serve a custom representation of the reference node", | |
"name" : "custom_representation", | |
"parameters" : [ ] | |
}[~/code/neo/server/examples] $ curl -X POST http://localhost:7474/db/data/ext/CustomRepresentation/graphdb/custom_representation | |
curl: option --X: is unknown | |
curl: try 'curl --help' or 'curl --manual' for more information | |
[~/code/neo/server/examples] $ curl -d "" http://localhost:7474/db/data/ext/CustomRepresentation/graphdb/custom_representation | |
{ | |
"hello_world" : "hello world from node 0" | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment