Select which attributes the GET returns from the list in Rest

0

Yours, I have the following doubt. I have an application in Rest that when I ask to return the list, it comes with many unnecessary fields, "example below". How do I select the attributes of the entity class that it should return?

My get:

@GetMapping
public List<Nota> listNotas(){
return notaRepository.findAll();
}

When I give a get I get the following return.

{
    "id": 28,
    "nota": 10,
    "dataNota": [
        2017,
        10,
        10
    ],
    "professor": {
        "id": 1,
        "nome": null,
        "matricula": null,
        "observacao": null,
        "dataNascimento": null,
        "ativo": null
    },
    "materia": {
        "id": 1,
        "nome": null
    },
    "aluno": {
        "id": 1,
        "nome": null,
        "matricula": null,
        "observacao": null,
        "dataNascimento": null,
        "ativo": null,
        "nomeResponsavel": null,
        "foneResponsavel": null
    }
}

I would like it to return with fewer fields such as

{
    "id": 28,
    "nota": 10,
    "dataNota": [
        2017,
        10,
        10
    ],
    "professor": {
        "id": 1,
        "nome": null,
               },
    "materia": {
           "id": 1,
           "nome": null
    },
    "aluno": {
        "id": 1,
        "nome": null,
        "matricula": null,
        }
}
    
asked by anonymous 12.08.2017 / 02:12

1 answer

0

Take a look at link

Basically:

1 - Create a class:

public class Views{
 public static class Public{}
 public static class General extends public{}
}

What is in public, will only appear when public and general what is empublic and general.

2 - In entities, note the attributes with:

ps: do this in tbm relationships

@JsonView(Views.Public.class) ou @JsonView(Views.General.class)

3 - And finally on getmapping

@GetMapping
@JsonView(Views.Public.class)
public List<Nota> listNotas(){
  return notaRepository.findAll();
}
    
12.08.2017 / 15:49