All I need some help to parse the object on the class base. I have a list of Imp object. i don't want it map all to the class and create the all object. I need only the First object of the Imp in my RtbRequest class.
Reason:- why i need to do like that. In imp json user send the non-number of imp object's in list and required on one object. i'd want to parse all. Is this possible
My Pojo Class
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RtbRequest {
// number of attribute
private Imp imp;
public void setImp(Imp imp) {
this.imp = imp;
}
}
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Imp {
// number of attribute
@JsonIgnore
private Map<String, String> impMap = new HashMap<>();
private String id;
private Double bidfloor;
//final-map
public void setId(String id) {
log.info("set--id--rtb--Imp");
this.id = id;
impMap.put("impid", getId());
}
}
My Json Object
{
"id": "ded06290-f586-45c6-bbcb-015adba03e39",
"imp": [{
"id": "1",
"video": {
"linearity": 1,
"maxduration": 120,
"protocols": [2, 5, 3, 6],
"w": 1280,
"h": 720,
"startdelay": 0,
"skip": 1,
"sequence": 1,
"playbackmethod": [1, 2, 3],
"api": [2]
},
"bidfloor": 0.0,
"secure": 1
},
{
"id": "2",
"video": {
"linearity": 1,
"maxduration": 120,
"protocols": [2, 5, 3, 6],
"w": 1280,
"h": 720,
"startdelay": 0,
"skip": 1,
"sequence": 1,
"playbackmethod": [1, 2, 3],
"api": [2]
},
"bidfloor": 0.0,
"secure": 1
}
]
}
My RestController
@RequestMapping(value= { "/request/{partner}"}, method = RequestMethod.POST)
public Map<String, String> getRtbResponse(@PathVariable String partner, @RequestBody RtbRequest request) {}
JSON serialization works best if you serialize into strong types, so each JSON object will normally map directly to a Java class. If all your items look like:
you should create an identical Java class, with getters and setters
The controller method signature should be:
If each partner sends in data in different formats, the solution may depend on weather you need to process the data, or simply store it. If you 'just' need to store the data and not process it, you can use the fact that all JSON object can be handled as maps, so it is possible to make your controller like this:
However if you need to process the data in any way, using
Map<String, Object>
is generally not a good plan.In Java we could create an
Interface PartnerItem
and multiple subclasses, each matching the data submitted by each partner. This is also possible in JSON, but since JSON objects have no name, it needs to be included in some other way. Here is a link to a guide that explains how Jackson inheritance worksNote: Personally I never use array as my top level JSON structure when I design APIs, I always use object. The reason is that I can add additional fields to an object, which allows me to mutate the API in a backwards compatible way - this is not possible if your top-level structure is an array.
All Sorry for late replay for this issue i handle this by use of Cross filter. I first update my IMP list to Object after that i update the ReadHttpServletRequest stream by adding this Request into do-filer change.
Main Key Point