I am using Spring 5.3.9, jackson-core and jackson-databind- 2.17.1 version. From UI, I am sending json array to the controller. I have tried multiple parameters types in the controller and none of them are working. Getting bad request.
@Controller
@RequestMapping("/secure")
public class PrsPyamentUtilizationSave {
@PostMapping(value = "/prsPyamentUtilSave.html", produces = MediaType.TEXT_HTML_VALUE)
@ResponseBody
public String prsPaymentUtilValidation(@RequestBody Map<String, Object> [] list) {
System.out.println("paymentUtils: " + paymentUtils);
return "OK";
}
}
if I change it to String it is working but output doesn’t seems like a json.
public String prsPaymentUtilValidation(@RequestBody String paymentUtils)
{
System.out.println("jsonArray: " + paymentUtils);
return "OK";
}
jsonArray: list%5B0%5D%5BReportId%5D=1234&list%5B0%5D%5BProviderId%5D=204437&list%5B0%5D%5BProgramTypeId%5D=1&list%5B0%5D%5BFcpUnits%5D=6&list%5B1%5D%5BReportId%5D=1234&list%5B1%5D%5BProviderId%5D=99999&list%5B1%5D%5BProgramTypeId%5D=1&list%5B1%5D%5BFcpUnits%5D=7
Changed to RestController as suggested.
@RestController
@RequestMapping("/secure")
public class PrsPyamentUtilizationSave {
@PostMapping(value = "/prsPyamentUtilSave.html")
public String prsPyamentUtilSave(@RequestBody List<PaymentDTO> paymenDTOs) {
System.out.println("jsonArray: " + paymenDTOs);
return "OK";
}
}
2
Answers
Updated the Ajax call by removing the key "list" and adding the "JSON.stringify" to the array object.
Also used RestController as suggested by Lane. Thanks Lane!!
Yes, a String will always work because it’s just that, a String. Your Map won’t work because your data isn’t structured as a Map. Consider making a DTO for this.
Then you just need to make your controller take a
List
of these.