Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jackson prints empty JSON string

I have a java program which should serialize objects using jackson (play framework). It was working, but I messed it up somehow and now I can't get it working. Here is my serializer

public String serializeObject(Object object) {
    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    String json = null;
    try {
        json = ow.writeValueAsString(object);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return json;
}

and here is the code which runs it:

return badRequest(serializeObject(new Error("bad input")));

and the error class:

public class Error {
    private String error;

    public Error(String error) {
        this.error = error;
    }
}

and as the output, all I get is { }

What is wrong?

like image 254
xpg94 Avatar asked Feb 23 '16 15:02

xpg94


1 Answers

Your Error Class's properties need to have setter and getters which you want to show in JSON output

public String getError() {
    return error;
}

public void setError(String error) {
    this.error = error;
}
like image 137
Ömer Erden Avatar answered Oct 04 '22 22:10

Ömer Erden