0

I implemented a rest controller in my SpringBoot backend, which receives a json and maps it to an dto object via SpringBoots default Jackson configuration:

So we got:

The Rescontroller signed like that:


public ResponseEntity<Void> pushMyDTO(@RequestBody MyDTO myDTO) {...}

The MyDTO Object :

@JsonInclude(JsonInclude.Include.NON_NULL)
public record MyDTO(int a, double b, double c, Map<String, List<MyObject>> myList) {}

And last but not least the MyObject:

public class MyObject{

    private double[] myArray = new double[2];
    private double a = 0;
...
}

I allready read the incoming json stream before Jackson touches it and it contains everything perfectly.

BUT:

After the Mapping Json --> MyDTO, the doubles in MyObject.myArray are all 0. The rest is fine. Those are the only attributes jackson isnt able to map somehow.

Im thankfull for every advice :)

New contributor
Michael Jackson-Mapping is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Answer 1

0

The issue happens because Jackson can’t bind to myArray — the field is private and your MyObject class doesn’t expose it as a property. Since there’s no setter/getter, Jackson ignores it and keeps the default value (new double[2], which is [0.0, 0.0]).

To fix it, just add getters and setters for myArray:

public class MyObject {

    private double[] myArray;
    private double a;

    public double[] getMyArray() {
        return myArray;
    }

    public void setMyArray(double[] myArray) {
        this.myArray = myArray;
    }

    public double getA() {
        return a;
    }

    public void setA(double a) {
        this.a = a;
    }
}

After adding these, Jackson will correctly map the array from your JSON.

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

1 Comment

MyObject allready looks like that. My bad, i should have posted the whole class... But Jackson, at least in Spring Boot, does not need any setters. Otherwise it would not be able to map my record neither, wouldnt it?

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.