spring控制器发出错误请求400

iqxoj9l9  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(305)

我经常将json作为请求体的一部分传递给spring控制器。不是为了我的生活,我不能让这个工作?给我400个坏请求。
Spring控制器:

@RequestMapping(value = "/saverefunds", method = RequestMethod.POST)
    @ResponseBody
    public String saveRefunds(Model model, HttpSession session, AS400WriteRefundLine as400WriteRefundLine, @RequestBody CreateRefund refunddetails) throws ParseException, IOException {
////
}

调用它的javascript代码:

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);
            }
        });

createrefund对象:

@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;
}

pojo与正在传递的json匹配,所以不确定它还能是什么?
有人看到什么明显的错误吗?
我可以传递数据的唯一方法是在spring控制器中使用map<string,string>,而不是createreturn对象…所以我假设jsonMap不起作用?
非常感谢

k0pti3hp

k0pti3hp1#

错误是在发送之前将数据转换为字符串。

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

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

到底是什么 AS400WriteRefundLine as400WriteRefundLine Spring又如何知道该把什么价值放在那里呢?

相关问题