MrHolal
MrHolal

Reputation: 339

Neo4j Spring JSON

Imagine i have a Person node who has 3 hobbies

Peter -> Hockey
Peter -> Soccer
Peter -> Basketball

When i call it from string from

interface PersonRepository extends Neo4jRepository<Person, Long>{

@Query("MATCH (p:Person)-[has:HAS_HOBBY]->(h:Hobby) RETURN p, has, h"
List<Person> find();

Then i have a PersonService method which calls PersonRepostiry.find()

And when i call this method from PersonService in controller

@RequestMapping("/")
public String all(){
    return personService.find();
}

It all works perfectly, but when i access this localhost:8080/ i get json like this

{
    "person":"Peter",
    "hobbies":{
          "name":"hockey"
     }, {
          "name":"soccer"
     }, {
          "name":"basketball"
     }
}

but i would really like to get output like this

{
    "person":"Peter",
    "hobby": { "name":"hockey" }
}
{
    "person":"Peter",
    "hobby": { "name":"soccer" }
}
{
    "person":"Peter",
    "hobby": { "name":"basketball" }
}

EDIT:

@NodeEntity
class Person{
    public Long id;
    public String name;

    @RelationShip(type="HAS_HOBBY")
    public Set<Hobby> hobbies;

    public addHobby(Hobby hobby) { TODO.. }
}

Upvotes: 0

Views: 105

Answers (1)

Eric Spiegelberg
Eric Spiegelberg

Reputation: 622

This type of JSON response creation could be done in your service or controller layer, iterating over what is returned from personService.find() and building a custom Map with the desired JSON shape.

Upvotes: 1

Related Questions