Why does ObjectMapper.writeValueAsString return []?

Advertisements

The below always returns a value of []
I’ve tried with both public and private params but no matter what its not working.
I’m intending to loop over an ArrayList of objects and convert them to an array of json for returning but it won’t even convert a single object.

I’ve tried removing the constructor and just using setters to set the params after creating the object. When I println the object I get QuizTable{id='foo', quizTitle='bar', ownerId='fizz'} so it does look to be creating the object

    @GetMapping("/getAll")
    public ArrayList<String> getAll(){
        ObjectMapper mapper = new ObjectMapper();
        QuizTable quiz = new QuizTable("foo", "bar", "fizz");
        String toJson = mapper.writeValueAsString(newQuiz);
        System.out.println(toJson);
        return toJson;
    }
public class QuizTable extends ArrayList<QuizTable> {

    public String id;
    public String quizTitle;
    public String ownerId;

    public QuizTable(String id, String title, String owner) {
        this.id = id;
        this.quizTitle = title;
        this.ownerId = owner;
    }

    @JsonIgnore
    public String everything(){
        return this.id;
    }

    @JsonIgnore
    public String asJson(){
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getQuizTitle() {
        return quizTitle;
    }

    public void setQuizTitle(String quizTitle) {
        this.quizTitle = quizTitle;
    }

    public String getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(String ownerId) {
        this.ownerId = ownerId;
    }

    @Override
    public String toString() {
        return "QuizTable{" +
                "id='" + id + '\'' +
                ", quizTitle='" + quizTitle + '\'' +
                ", ownerId='" + ownerId + '\'' +
                '}';
    }
}

>Solution :

You are extending ArrayList<QuizTable> for no apparent reason.
So your class QuizTable has extended properties of an ArrayList class.
You are getting an empty array as JSON because of extending this class because ObjectMapper is converting it as an ArrayList.

Let’s look at an example here:

I have created a TestClass

public class TestClass extends ArrayList<TestClass> {

    private final String firstName;

    public TestClass(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return firstName;
    }
}

Lets create another class:

public class MainClass {

    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();
        TestClass quiz = new TestClass("foo");
        try {
            String toJson = mapper.writeValueAsString(quiz);
            System.out.println(toJson);
        } catch (JsonProcessingException j) {
            System.out.println("E");
        }
    }

}

The output will be:
[]

But as soon as you remove the extends ArrayList<TestClass> or use extends Object or don’t extend anything:

public class TestClass {

    private final String firstName;

    public TestClass(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return firstName;
    }

}

You get your desired output:
{"firstName":"foo"}

Leave a ReplyCancel reply