jackson 为什么ObjectMapper.writeValueAsString返回[]?

klh5stk1  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(85)

下面的总是返回一个值[]我已经尝试了公共和私有参数,但无论如何它都不起作用。我打算循环遍历对象的ArrayList,并将它们转换为JSON数组以返回,但它甚至不会转换单个对象。
我尝试删除构造函数,在创建对象后只使用setter来设置参数。当我打印对象时,我得到QuizTable{id='foo', quizTitle='bar', ownerId='fizz'},所以它看起来确实是在创建对象

@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 + '\'' +
                '}';
    }
}
ojsjcaue

ojsjcaue1#

您正在扩展ArrayList<QuizTable>,没有明显的原因。因此,类QuizTable具有ArrayList类的扩展属性。由于ObjectMapper将其转换为ArrayList,因此扩展此类会得到一个空数组作为JSON。
让我们来看看这里的一个例子:
创建一个TestClass

public class TestClass extends ArrayList<TestClass> {

    private final String firstName;

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

    public String getFirstName() {
        return firstName;
    }
}

创建另一个类:

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");
        }
    }

}

输出将为:[]
但是一旦你删除extends ArrayList<TestClass>或使用extends Object或不扩展任何东西:

public class TestClass {

    private final String firstName;

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

    public String getFirstName() {
        return firstName;
    }

}

您将获得所需的输出:{"firstName":"foo"}

相关问题