Issue
I'm trying to send data to my controller, have it do a service call and then return the result to be displayed on the view
My Ajax call looks like this
$.ajax({
url: "<c:url value="submitReportQuery"/>",
type: "POST",
dataType: "html",
contentType: "application/json;",
data: JSON.stringify(reportQueryMap),
success: function (data) {
$('#SelfServiceResults').html(data);
}
})
and my controller looks like this
@RequestMapping(value = "submitReportQuery", method = RequestMethod.POST, consumes="application/json" )
public String submitReportQuery(@ModelAttribute Map<String, String> reportQueryMap/*, Model model, BindingResult bindingResult*/)throws Exception{
//model.addAttribute("successful", true);
return "queries/SelfServiceQueriesSubmitResults";
}
The json object looks like this (it can have anything from 0-5 randomKeys) which I pass as a map to a service Note: the actual name of "randomKey" can change the key is a unknown
{
"randomKey1":"111",
"randomKey2":"222",
"randomKey3":"333",
"reportType":"Commission Processed BH",
"reportProduct":"LIFE",
"reportMonth":"January 2017",
"queryRemark":"nice"
}
I can't seem to get the "successful" attribute passed to the view, if I add the parts comented out I get this error
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoSuchMethodError: java.lang.String submitReportQuery(java.util.Map)
I basically want This but it must return a view with attributes set on it
Solution
@RequestMapping(value = "submitReportQuery", method = RequestMethod.POST, consumes="application/json" )
public String submitReportQuery(@RequestBody ReportQueryMapBean reportQueryMap)throws Exception{
//model.addAttribute("successful", true);
return "queries/SelfServiceQueriesSubmitResults";
}
public class ReportQueryMapBean {
// to do delcare the bean fields to match the request
// and corresponding getter and setter function
}
Answered By - Gnanasekaran Palanisamy
Answer Checked By - David Goodson (JavaFixing Volunteer)