2

I'm trying to convert a JSON object into a POJO class, but some values are ending up with null values while others are set to the value from the JSON. The response is

{
"intention_id": 12936,
"intention_datetime": "2021-03-09T09:58:16-03:00",
"status": "created",
"cuit": "20-314559999-9",
"branch": "Avellaneda 13",
"checkout": "8",
"terminal_number": "87980012",
"intention_type": "payment",
"original_amount": 1200.0,
"currency": "ARS",
"payment_methods_data": [
    {
        "payment_method_id": 1,
        "establishment_id": "29880765",
        "trace_number": 1090,
        "ticket_number": 131,
        "installments": [
            {
                "quantity": 1,
                "total_amount": 1200.0,
                "amount_per_installment": 200.0,
                "cft": 12.56,
                "tna": 14.87
            },
            {
                "bank_id": 28,
                "quantity": 6,
                "total_amount": 1200.0,
                "amount_per_installment": 200.0,
                "cft": 12.56,
                "tna": 14.87
            },
            {
                "quantity": 6,
                "total_amount": 1400.0,
                "amount_per_installment": 233.33,
                "cft": 12.56,
                "tna": 14.87
            }
        ]
    },
    {
        "payment_method_id": 15,
        "establishment_id": "69880548",
        "trace_number": 2034,
        "ticket_number": 673,
        "installments": [
            {
                "quantity": 1,
                "total_amount": 1200.0,
                "amount_per_installment": 200.0,
                "cft": 12.56,
                "tna": 14.87
            },
            {
                "bank_id": 28,
                "quantity": 6,
                "total_amount": 1200.0,
                "amount_per_installment": 200.0,
                "cft": 12.56,
                "tna": 14.87
            },
            {
                "quantity": 6,
                "total_amount": 1400.0,
                "amount_per_installment": 233.33,
                "cft": 12.56,
                "tna": 14.87
            }
        ]
    }
],
"integrator_intention_id": "587cba61-ff27-46c6-ba8b-fd936665c35f",
"integrator": "alsea"
}

I'm ignoring the array elements, so my Object Class is

    public class ObjectResponseBimo {
    
    public int intention_id;    
    public String intention_datetime;   
    public String status;   
    public String cuit;
    public String branch;   
    public String checkout; 
    public String terminal_number;  
    public String intention_type;   
    public double original_amount;  
    public String currency; 
    public String integrator_intention_id;  
    public String integrator;

    public int getIntention_id() {
        return intention_id;
    }
    @JsonProperty("intention_id")
    public void setIntention_id(int intention_id) {
        this.intention_id = intention_id;
    }
    public String getIntention_datetime() {
        return intention_datetime;
    }
    @JsonProperty("intention_datetime")
    public void setIntention_datetime(String intention_datetime) {
        this.intention_datetime = intention_datetime;
    }
    public String getStatus() {
        return status;
    }
    @JsonProperty("status")
    public void setStatus(String status) {
        this.status = status;
    }
    public String getCuit() {
        return cuit;
    }
    @JsonProperty("cuit")
    public void setCuit(String cuit) {
        this.cuit = cuit;
    }
    public String getBranch() {
        return branch;
    }
    @JsonProperty("branch")
    public void setBranch(String branch) {
        this.branch = branch;
    }
    public String getCheckout() {
        return checkout;
    }
    @JsonProperty("checkout")
    public void setCheckout(String checkout) {
        this.checkout = checkout;
    }
    public String getTerminal_number() {
        return terminal_number;
    }
    @JsonProperty("terminal_number")
    public void setTerminal_number(String terminal_number) {
        this.terminal_number = terminal_number;
    }
    public String getIntention_type() {
        return intention_type;
    }
    @JsonProperty("intention_type")
    public void setIntention_type(String intention_type) {
        this.intention_type = intention_type;
    }
    public double getOriginal_amount() {
        return original_amount;
    }
    @JsonProperty("original_amount")
    public void setOriginal_amount(double original_amount) {
        this.original_amount = original_amount;
    }
    public String getCurrency() {
        return currency;
    }
    @JsonProperty("currency")
    public void setCurrency(String currency) {
        this.currency = currency;
    }
    public String getIntegrator_intention_id() {
        return integrator_intention_id;
    }
    @JsonProperty("integrator_intention_id")
    public void setIntegrator_intention_id(String integrator_intention_id) {
        this.integrator_intention_id = integrator_intention_id;
    }
    public String getIntegrator() {
        return integrator;
    }
    @JsonProperty("integrator")
    public void setIntegrator(String integrator) {
        this.integrator = integrator;
    }
    
    @Override
    public String toString() {
        return "ObjectResponseBimo [intention_id=" + intention_id + ", intention_datetime=" + intention_datetime
                + ", status=" + status + ", cuit=" + cuit + ", branch=" + branch + ", checkout=" + checkout
                + ", terminal_number=" + terminal_number + ", intention_type=" + intention_type + ", original_amount="
                + original_amount + ", currency=" + currency + ", integrator_intention_id=" + integrator_intention_id
                + ", integrator=" + integrator + "]";
    }}

All the other getter and setter, everyone has JsonProperty in the set method

I'm working with Jackson, so I'm sending the object with

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
ObjectResponseBimo o = objectMapper.readValue(jsonToPost, ObjectResponseBimo.class);
return objectMapper.toString() + "\n" + o.toString();

The output is:

ObjectResponseBimo [intention_id=0, intention_datetime=null, status=null, cuit=20-314559999-9, branch=Avellaneda 13, checkout=8, terminal_number=87980012, intention_type=payment, original_amount=1200.0, currency=ARS, integrator_intention_id=587cba61-ff27-46c6-ba8b-fd936665c35f, integrator=null ]

4
  • Could you please post the complete entity class ObjecrResponseBimo. Also try using the getters to get the individual property values and check the toString method. Commented Mar 9, 2021 at 16:23
  • @Ironluca I've edited the post Commented Mar 9, 2021 at 16:32
  • The problem that you are experiencing is not caused by any of the code that you included. I took your code and your example JSON and it parses correctly. Commented Mar 9, 2021 at 17:17
  • @ClaudioScabbiaSepúlveda, With 2.7, it parses fine as DwB mentioned. Which version of Jackson are you using? JsonAutoDetect is an annotation in 2.x, Are you using 1.x? Commented Mar 9, 2021 at 18:29

2 Answers 2

1

The code you provided works fine. I have just copied it to the clean project and tested it.

I would give you couple of notes, not related to the question.

  • Class variables should be private, and for Jackson no need to have setters and getters to make json. It used reflection.
  • Class variables should be CamelCase. This is general practice in Java.

This is is, how your data class should look like:

public class ObjectResponseBimo {

    @JsonProperty("intention_id")
    private int intentionId;
    @JsonProperty("intention_datetime")
    private String intentionDatetime;
    private String status;
    private String cuit;
    private String branch;
    private String checkout;
    @JsonProperty("terminal_number")
    private String terminalNumber;
    @JsonProperty("intention_type")
    private String intentionType;
    @JsonProperty("original_amount")
    private double originalAmount;
    private String currency;
    @JsonProperty("integrator_intention_id")
    private String integratorIntentionId;
    private String integrator;
}

And to work with json, you could use a tool to hide all routinse. E.g. JacksonUtils:

ObjectResponseBimo obj = JacksonUtils.readValue(json, ObjectResponseBimo.class);

P.S. Repeat, code that you have provided works fine on the clean project. Probably you have a problem someqhere else, and this is only the side effect.

Sign up to request clarification or add additional context in comments.

Comments

0

So I just tried to copy your code and made couple of changes.

  1. Add below annotation at the top of your pojo class (ObjectResponseBimo.java)
  **@JsonIgnoreProperties(ignoreUnknown = true)**
public class ObjectResponseBimo {

This will ignore the unknown properties from json having no match with equivalent java properties.

  1. And for the object mapper, below snippet should be enough to get it work:
ObjectMapper objectMapper = new ObjectMapper();
ObjectResponseBimo ob = objectMapper.readValue(ClassLoader.getSystemResourceAsStream("ObjectResponseBimo.json"), ObjectResponseBimo.class);
System.out.println(ob.toString());
System.out.println(ob.getIntention_id() + " | " + ob.getBranch());

I am not sure why toString() is not working for you. It is working for me though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.