![](/img/trans.png)
[英]400 Bad Request When Send Multipart Request To Spring MVC Controller
[英]Spring controller give bad request 400
我经常将 JSON 作为请求正文的一部分传递给我的 spring 控制器。 不是为了我的生活,我不能让这个工作吗? 给我 400 个错误的请求。
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 代码:
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 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;
}
POJO 与正在传递的 JSON 匹配,所以不确定它还能是什么?
有人看到有什么明显的错误吗?
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?
非常感谢
错误是您在发送之前将数据转换为字符串。
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 如何知道放在那里的价值?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.