I'm manipulating the following JSON object in my website frontend
<script>
window.campaign = {
name: "test",
budget: 20.00,
type: 0,
dynamic: false,
geo_target: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27],
time_target: {
monday: ["00","00","00","00",1],
tuesday: ["00","00","00","00",1],
wednesday: ["00","00","00","00",1],
thursday: ["00","00","00","00",1],
friday: ["00","00","00","00",1],
saturday: ["00","00","00","00",1],
sunday: ["00","00","00","00",1]
},
setName: function(val) {
this.name = val;
},
//some more setters go here
}
</script>
Then I send it to my Spring application using AJAX
$.ajax({ type: "POST", url: submit_url, data: JSON.stringify(window.campaign) })
.done(function() { alert( "success" ); })
.fail(function() { alert( "error" ); });
Now the problem is mapping all the variables accordingly in my @Controller
....
@RequestParam(value = "name", required = false, defaultValue = "") String name,
@RequestParam(value = "budget", required = false, defaultValue = "0") BigDecimal budget,
@RequestParam(value = "type", required = false, defaultValue = "0") int type,
@RequestParam(value = "dynamic", required = false, defaultValue = "false") boolean dynamic,
@RequestParam(value = "geo_target", required = false, defaultValue = "") int[] geoTargeting,
....
This all works fine, but I have no idea how to map the time_target
I tried creating a new Model and mapping it using @RequestBody
....
@RequestParam(value = "name", required = false, defaultValue = "") String name,
....
@RequestParam(value = "geo_target", required = false, defaultValue = "") int[] geoTargeting,
@RequestBody TimeTarget timeTargeting
....
with no success. I used http://www.jsonschema2pojo.org to create a Class for the whole object I'm sending, but no success whatsoever (just FYI, it created two classes, one for timeTargeting and one for everything else).
I'm getting really desperate. Help, please :) If the code provided is not enough, I can update with more.
@RequestParam(value = "time_target") String[] timeTargetingthis work for you ?