Use same REST representations using Graphaware Neo4j Framework as the ones we get from the Neo4j REST API

223 views Asked by At

I am developing a Neo4j server extension using the Neo4j Framework provided by Graphaware.

I want in my response to send the following object (simplified so that you can see the attributes) :

public class DiffResult {
    private Node fileOrFolder;
    private Node originalContent;
    private Path path;
}

The problem is that the Node object cannot be rendered by Jackson. I have seen a NodeRepresentation class somewhare but I also don't know how to use it properly with my Spring MVC Controller.

I want my nodes to be serialized like in the Neo4j REST Api (cf documentation: http://neo4j.com/docs/stable/rest-api-nodes.html#rest-api-get-node)

I also show you the controller I am using (also simplified).

@Controller
@RequestMapping("/diff")
public class FileSpaceDiffApi {

    private final GraphDatabaseService database;

    @Autowired
    public FileSpaceDiffApi(GraphDatabaseService database) {
        this.database = database;
    }

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public List<DiffResult> diff(@QueryParam("fileSpaceId") Long fileSpaceId, @QueryParam("since") Long since) {
        List<DiffResult> results = new ArrayList<DiffResult>();
        Transaction tx = database.beginTx();
        try {
            Node startNode = database.getNodeById(fileSpaceId);
            DiffResult diffResult = new DiffResult();
            diffResult.setFileOrFolder(startNode);
            results.add(diffResult);
            tx.success();
        }
        finally {
            tx.close();
        }
        return results;
    }

}

Ideally I'd also like to be able to render the Path in JSON.

1

There are 1 answers

0
Michal Bachman On BEST ANSWER

There is (yet) no capability of easily returning nodes in the same format as Neo4j does. This is mainly because the Neo4j REST API is very generic and thus too chatty and verbose for many use-cases.

I would suggest looking at com.graphaware.api.JsonNode to which you can pass a Neo4j node and some configuration about what will be present in the generated JSON (e.g. whether to include labels, etc.)

You can use it by adding the following to your pom.xml:

 <dependency>
    <groupId>com.graphaware.neo4j</groupId>
    <artifactId>api</artifactId>
    <version>${graphaware.version}</version>
 </dependency>

As for paths, there is a JsonPath class in neo4j-algorithms, that will help you achieve what you want. We will happily move to the core framework for the next release (that's where it really should be).