2

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

Federico Zancan
  • 4,846
  • 4
  • 44
  • 60
Steve Green
  • 79
  • 1
  • 10

1 Answers1

0

The error is that you convert your data to string before sending.

  await $.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            url: "saverefunds",
            data: refunddetails,  <---------pass the json object here
            success: function (data) {

Also

public String saveRefunds(Model model, HttpSession session, AS400WriteRefundLine as400WriteRefundLine, @RequestBody CreateRefund refunddetails) throws ParseException, IOException {
////

What exactly is AS400WriteRefundLine as400WriteRefundLine and how will spring know what value to put there?

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
  • This is not so. jQuery will accept a string as the data parameter and simply pass that along as the body of the POST request. "_When data is passed as a string it should already be encoded using the correct encoding for contentType_" from here: https://api.jquery.com/jQuery.ajax/ - in this case the contentType matches the JSON encoding. – Randy Casburn Mar 21 '21 at 23:01
  • A similar issue was already solved here. https://stackoverflow.com/a/10215019/7237884 I am pretty sure that this is the issue, in reality I had the same issue in the past – Panagiotis Bougioukos Mar 21 '21 at 23:05
  • `.toString()` does not produce JSON - it is _not_ the same issue. – Randy Casburn Mar 22 '21 at 00:27