0

I'm using Spring as backend and Angular as frontend.

This is my REST code:

    @GetMapping(path = "/endpoint")
    public @ResponseBody Iterable<Relations> getGraphGivenEndpointId(@RequestParam(value = "id") int id) {
        return relationsRepository.findAllRelationsGivenEndpointId(id);

    }

When I call it using Angular it returns this JSON:

0: 
{  id: 27,
   resourceUri:"http://datiopen.istat.it/odi/ontologia/microdati/musei/Visita",
   endpoint: {id: 1, status: "ANALISI COMPLETATA", endpointUri: "http://datiopen.istat.it/sparql/musei"},
   classStructure:{classUri:"http://datiopen.istat.it/odi/ontologia/microdati/musei/Visita",status: "ANALISI COMPLETATA",id: 6,endpoint: {id: 1, status: "ANALISI COMPLETATA", endpointUri: "http://datiopen.istat.it/sparql/musei"}},
   predicateStructure:{id: 10, classStructure: classUri:"http://datiopen.istat.it/odi/ontologia/microdati/musei/Visita", status: "ANALISI COMPLETATA", id: 6, endpoint: {…}},
    predicateUri: "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",status: "ANALISI COMPLETATA"}
}

But I need this JSON:

Edges[] = [
  {
    id: 'a',
    source: '1',
    target: '2',
    label:'predicate'
  }
];

Nodes[] = [
  {
    id: '1',
    label: 'Node A'
  },
{
    id: '2',
    label: 'Node B'
  }
];

In particular I need to get this JSON:

Edges[] = [
  {
    id: 'a',
    source: '1',
    target: '2',
    label:'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
  }
];

Nodes[] = [
  {
    id: '1',
    label: 'http://datiopen.istat.it/odi/ontologia/microdati/musei/Visita'
  },
{
    id: '2',
    label: 'http://datiopen.istat.it/odi/ontologia/microdati/musei/Visita'
  }
];

Q1. This change of the JSON structure is better to do it in backend (Spring) or frontend (Angular)?

Q2. How can I do this change of the JSON structure in Spring or Angular?

Thanks in advance.

Lulixter
  • 161
  • 1
  • 3
  • 12
  • Do it in backend. provide the backend source code for ```relationsRepository.findAllRelationsGivenEndpointId(id);``` method – Argon Feb 13 '20 at 11:59

2 Answers2

0

I think what you need is the concept of a DTO (Data Transfer Object), which you can use to encapsulate the data in the format that you need.

Personally I would do this at your Spring backend and normally what I would do is introduce a service layer to the application that is between your web layer (controller) and your data repository, I would recommend you have a look into this pattern.

This way you have the flexibility to tailer the data format that you send back to the client (or multiple clients).

But in your instance and example you could do the following

Create DTO objects, modelled around the response structure you require i.e.

public class EdgeDto {
    private String id;
    private String source;
    private String target;
    private String label;

   //Getters and Setters
}

public class NodeDto {
    private String id;
    private String label;

   //Geters and Setters
}

public class RelationDto {
    private List<EdgeDto> edges;
    private List<NodeDto> nodes;

   //Getters and Setters
}

Then in your controller

 @GetMapping(path = "/endpoint")
    public @ResponseBody Iterable<RelationDto> getGraphGivenEndpointId(@RequestParam(value = "id") int id) {

     Iterable<Relations> relations = relationsRepository.findAllRelationsGivenEndpointId(id);
     Iternable<RelationDto> relationDtos = mapToRelationsDto(relations);
     return relationDtos;
    }

However as mentioned above, you may want to introduce a service inbetween your controller and the database, which will give you more control and better reuse of any business logic etc that you may require.

Brendon Randall
  • 1,436
  • 3
  • 14
  • 25
  • hi, thanks for your answer. I use spring so I would like to know if i have to use some annotation on this DTO classes. – Lulixter Feb 13 '20 at 13:11
  • No annotations neccessary as this is just a design pattern, so nothing spring provides out of the box for stereotyping – Brendon Randall Feb 13 '20 at 13:17
0

I think you have a couple of possibilities here, all on the backend that is the one that creates the JSON, if you handle on Angular, you are not changing the JSON you are just modifying the way you receive the data.

  1. You can create 1 or more DTO with the structure you want to send, in this case, you should create a ResponseDTO with contains a list of EdgeDTO and a list of NodesDTO.
  2. You can try to use Jackson with @JsonProperty and @JsonAlias to try to change your current object (probably is complex in your case)
  3. You can create the JSON manually and then send it, as explained Create JSON object using Jackson in Java

Probably doing the DTO is the best way, but in my experience is better to keep as similar to your domain as possible if not would make it more complex in the future to maintain.

Malbein
  • 1
  • 1