How to manipulate a Json object before returning to the API

0

Hello

I'm using this method in my controller:

@GetMapping(value="/{id}/perfil", produces = MediaType.APPLICATION_JSON_VALUE)
public String carregarPerfilUsuario(@PathVariable("id") Long id, HttpServletResponse response) throws IOException {

    obj.put("usuario", ur.findWhereId(id));
    obj.put("postagem", pr.findAllWhereUserId(id));

    return gson.toJson(obj);

}

I'm using gson to transform an object into json and JsonOBJECT to create an obj that will be returned at the end:

private static final Gson gson = new Gson();
private static final JSONObject obj = new JSONObject();

But I do not know why converting my data inserts unwanted attributes into my api:

Map and MyArrayList , I wanted to not see these attributes when I converted, how can I?

The result I'm looking for is similar to the image, I just wish it did not have these attributes that I commented in bold in the question.

    
asked by anonymous 15.10.2018 / 03:49

1 answer

0

Hello,

The methods you are using suggest that you have a Map (put method) and an ArrayList (findAll method), hence your map and your myArrayList.

The right thing to do is to have a POJO that represents the object to be returned by your API, and populate it with the user and post data.

For example:

public class PostagemDTO {

   private Usuario usuario;
   private List<Postagem> postagens;

   // getters e setters

}

So your API will return the desired result.

I hope it helps!

    
16.10.2018 / 02:09