How to mount a Json with Jackson having the model classes?

Asked

Viewed 89 times

0

I have the following classes:

public class OnesignalRequestModel {

    private String app_id;
    private List<IDModelJson> include_player_ids;
    private ContentJsonModel content;

    public String getApp_id() {
        return app_id;
    }

    public void setApp_id(String app_id) {
        this.app_id = app_id;
    }

    public List<IDModelJson> getInclude_player_ids() {
        return include_player_ids;
    }

    public void setInclude_player_ids(List<IDModelJson> include_player_ids) {
        this.include_player_ids = include_player_ids;
    }

    public ContentJsonModel getContent() {
        return content;
    }

    public void setContent(ContentJsonModel content) {
        this.content = content;
    }
}

public class IDModelJson {

    private Integer id_onesignal;

    public Integer getId_onesignal() {
        return id_onesignal;
    }

    public void setId_onesignal(Integer id_onesignal) {
        this.id_onesignal = id_onesignal;
    }
}

class ContentJsonModel {

    public String getEn() {
        return en;
    }

    public void setEn(String en) {
        this.en = en;
    }

    private String en;
}

And I would like to build a JSON in this style (I rode in Python so easily...)

{"app_id": "app_id_do_usuario", "include_player_ids": [id_onesignal], "contents": {"en": mensagem}}

But I don’t understand how I can effectively assemble it using Jackson and taking advantage of the models I’ve written.

The class I want to implement is this one:

public class RequestOneSignal {

    final ObjectMapper mapper = new ObjectMapper();

    public int doPost() throws IOException {
        HttpClient client = HttpClientBuilder.create().build();
        try (CloseableHttpClient clientClose = HttpClients.createDefault()) {
            HttpPost post = new HttpPost("url");
            post.setHeader("Content-Type", "application/json; charset=utf-8");
        }
    }

    private HttpEntity createEntity(List<Integer> clientsID){
        //Aqui, quero gerar o JSON e poder setar numa entity para fazer o post

    }

}

1 answer

0

Hey, good night, man. Assemble all your answer (Onesignalrequestmodel) and send it with the command

OnesignalRequestModel onesignalRequestModel = new OnesignalRequestModel();
.. sete os atributos
mapper.writeValue(new File("caminhoDoArquivo.json"),onesignalRequestModel);

Take a look here https://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

I hope it helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.