I often pass JSON to my spring controllers as part of the request body. Not for the life of me, I cannot get this one to work? Give me 400 bad request.
Spring controller:
@RequestMapping(value = "/saverefunds", method = RequestMethod.POST)
@ResponseBody
public String saveRefunds(Model model, HttpSession session, AS400WriteRefundLine as400WriteRefundLine, @RequestBody CreateRefund refunddetails) throws ParseException, IOException {
////
}
Javascript code which makes a call to it:
let refunddetails = {
blackbookno: refundedInvoiceNo,
RfExno: "00",
transactionType: "R",
stockid: stockid,
sku: sku,
refundtype: refundtype,
refundreason: refundreason,
btsflag: btsflag,
uniqueid: uniqueid,
thisrefundamount: thisrefund,
firstrecord: firstRecord.toString(),
nextRfEx:"1",
firstname: wFname,
surname: wLname,
address1: wAdd1,
address2: wAdd2,
town: wTown,
county: wCounty,
landline: wTelno,
mobile: wMobile,
email: wEmail
};
await $.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: "saverefunds",
data: JSON.stringify(refunddetails),
success: function (data) {
alert(data);
if (data.substring(0, 2) != "OK:") {
// ERROR
alert("postErr: " + data);
} else {
alert('Refund Generated');
}
return true;
},
error: function (error) {
alert('ERROR:'+error.responseText);
}
});
The CreateRefund object:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties
public class CreateRefund {
private String blackbookno, RfExno, transactionType, stockid, sku, refundtype, refundreason, btsflag,
uniqueid, thisrefundamount, firstrecord, nextRfEx, firstname,
surname, address1, address2, town, county, landline, mobile, email;
}
The POJO matches the JSON being passed so not sure what else it could be?
Anybody see anything obviously wrong?
The only way I can pass the data across is by using Map<String,String> in the spring controller as opposed to CreateRefund object...So I'm assuming the json mapping isn't working?
Many thanks