Search code examples
javajsonpojo

Java POJO Modelling


I want a POJO to contain a Status field (only two possible values SUCCESS, FAILED) and depending on this status, the second field needs to be

  1. either a POJO object( if Status is SUCCESS)
  2. or just String error message(if Status is FAILED)

What should be the data type of the second field in POJO?

public class MyBean {
    private Status status,
    private ??? body
}

Solution

  • JSON is also a String, So you can define it as String itself.

    When exchanging data between a browser and a server, the data is mostly text and represented in JSON format.

    public class MyBean {
        private Status status;
        private String body;
    }