skip to Main Content

I have a Bean class which I have constructed according to the response I have got. Below is my Postman response.

{
    "EU": [
        {
            "calId": "EU",
            "calDate": "2022-11-01",
            "prevBusinessDay": "2022-11-01",
            "nextBusinessDay": "2022-11-01",
            "businessDay": true,
            "monthEndBusinessDay": false
        }
    ],
    "AU": [
        {
            "calId": "AU",
            "calDate": "2022-11-01",
            "prevBusinessDay": "2022-11-01",
            "nextBusinessDay": "2022-11-01",
            "businessDay": true,
            "monthEndBusinessDay": false
        }
    ]
}

According to this I have constructed the Bean as follows

@Data
@AllArgsConstructor
@NoArgsConstructor

public class IndexCalendarDateResponseBean {
    @JsonProperty("EU")
    private List<IndexCalendarDateResponseWrapper> EU;
    
    @JsonProperty("AU")
    private List<IndexCalendarDateResponseWrapper> AU;

}

IndexCalendarDateResponseWrapper is another POJO which contains all the variables calId,calDate etc which are inside the two JSON arrays.

My issue here is every time this array names cannot be EU and AU. This depends on whatever I pass in the API request URI path parameters. If I pass something else, then that will be the JSON array variable names. So my bean will not work for that. So I wanted to construct a bean that should be dynamic in nature. What I am doing with this bean is I am using this bean to fetch an API response as follows.

IndexCalendarDateResponseBean actualRIOutput = JsonPath.from(response.extract().asInputStream()).getObject("", IndexCalendarDateResponseBean.class);

So how to construct a bean such that even if I pass say something else as path parameters say JU, BU, and SU or JU and BU anything, it will fetch the response for me? Also the JSON array variables I am passing in path parameters can also vary in quantity i.e. it can be two, three or any number of paramaters I can pass. So that also the bean should accept? Is any other bean needed for this idea to support?

2

Answers


  1. Why not replace IndexCalendarDateResponseBean with Map<Stirg,List< IndexCalendarDateResponseWrapper>>。

        private static ObjectMapper mapper = new ObjectMapper();
    
        static {
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        }
    
        public static <T> T parseObject(String json, TypeReference<T> typeRef) throws Exception {
            return mapper.readValue(json, typeRef);
        }
    
        ....
    
        return JsonUtils.parseObject(jsonStr, new TypeReference<Map<String, List<IndexCalendarDateResponseWrapper>>>() {
            });
    
    Login or Signup to reply.
  2. So how to construct a bean such that even if I pass say something else as path parameters say JU, BU, and SU or JU and BU anything, it will fetch the response for me?

    You can create a field of type Map<String, List<IndexCalendarDateResponseWrapper>> and make use of the method annotated with @JsonAnySetter to populate it.

    If you need the ability to serialize this POJO into the same JSON, then you can add another method exposing the data of the Map annotated with @JsonAnyGetter.

    public class IndexCalendarDateResponseBean {
        private Map<String, List<IndexCalendarDateResponseWrapper>> responseWrappers = new HashMap<>();
        
        @JsonAnySetter
        public void readResponseWrappers(String key, List<IndexCalendarDateResponseWrapper> value) {
            responseWrappers.put(key,value);
        }
    
        @JsonAnyGetter
        public Map<String, List<IndexCalendarDateResponseWrapper>> writeResponseWrappers() {
            return responseWrappers;
        }
    }
    

    Also the JSON array variables I am passing in path parameters can also vary in quantity i.e. it can be two, three or any number of paramaters

    When you’re deserializing JSON into a POJO missing properties would be set to default values of the corresponding type (for instance, null for reference types).

    When you’re serializing a POJO into JSON if you don’t want to include null fields you can make use of the annotation @JsonInclude providing the value of JsonInclude.Include.NON_NULL or JsonInclude.Include.NON_EMPTY (depending on your requirements).

    Example:

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class IndexCalendarDateResponseWrapper {
        // field, constructors, etc.
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search