Skip to content
Advertisement

Java Spring omits fields when making a JSON

public class ConnectedEntry {

    private EntryInScores scores;
    private EntryInValues values;
    private String someString;

    public ConnectedEntry(EntryInScores scores, EntryInValues values) {
        this.scores = scores;
        this.values = values;
        this.someString = "Adasd";
    }

I have an object that looks more or less like this, and I use it as a GET response for my API. scores and values are both database entities. I wanted to add a String to the response with some additional information.

What happens is that the objects are properly turned into a JSON and they show up in the response, but the string is omitted, with no error: it’s just not there.

I tried wrapping the string in a wrapper class, but it didn’t help.

What could I do?

Advertisement

Answer

Usually Spring uses Jackson as the default converter from objects to JSON. In order for Jackson to convert to JSON you must provide getters, so that Jackson can get and convert those values. As I can see in your representation you don’t have any getters. Try providing getters for the fields that you wish to convert. Never make fields public!

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement