7

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?

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
xpg94
  • 495
  • 1
  • 10
  • 26

1 Answers1

13

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;
}
Ömer Erden
  • 7,680
  • 5
  • 36
  • 45