0

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

LegionDev
  • 1,391
  • 4
  • 15
  • 29
  • I think you should use `@RequestBody` rather than `@ModelAttribute`, and it would be better to use a class rather than a Map (just my thoughts). – Luiggi Mendoza Jan 05 '18 at 07:09

1 Answers1

1
@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
}
  • I get that same "java.lang.NoSuchMethodError: java.lang.String submitReportQuery(java.util.Map)" when trying this solution :( – LegionDev Jan 05 '18 at 08:42
  • you change ReportQueryMapBean to Map – Gnanasekaran Palanisamy Jan 05 '18 at 09:18
  • I got it working with the ReportQueryMapBean, but all the values seem to be null – LegionDev Jan 05 '18 at 12:20
  • Try the below changes `public class ReportQueryMapBean { private String randomKey1; private String randomKey2; private String randomKey3; private String reportType; private String reportProduct private String reportMonth; private String queryRemark; // to do add getter and setter method for the above fields }` – Gnanasekaran Palanisamy Jan 05 '18 at 12:27