Javascript 向弹簧控制器发送DTO后,参数为空
我试图将Javascript前端的字符串列表发布到Spring引导控制器,由于某种原因,发布会发生,但我的值为null。我的代码有什么明显的错误吗 前端:Javascript 向弹簧控制器发送DTO后,参数为空,javascript,java,spring-boot,Javascript,Java,Spring Boot,我试图将Javascript前端的字符串列表发布到Spring引导控制器,由于某种原因,发布会发生,但我的值为null。我的代码有什么明显的错误吗 前端: let bemsidList = new Array() bemsidList[0] = "3129426"; bemsidList[1] = "240540"; let postData = { bemsids: bemsidList}; var xhr = new XMLHttpRequest()
let bemsidList = new Array()
bemsidList[0] = "3129426";
bemsidList[1] = "240540";
let postData = { bemsids: bemsidList};
var xhr = new XMLHttpRequest();
xhr.open("POST", "/admin/delete-email", true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
postData
}));
包装器:
public class EmailWrapper {
List<String> bemsids;
public List<String> getBemsids() {
return bemsids;
}
public void setBemsids(List<String> bemsids) {
this.bemsids = bemsids;
}
}
public类EmailWrapper{
列出bemsid;
公共列表getBemsids(){
返回bemsid;
}
公共空间管理系统(列表BEMSID){
this.bemsids=bemsids;
}
}
控制器:
@RequestMapping(
value = "/admin/delete-email",
method = RequestMethod.POST,
consumes = "application/json")
public String deleteEmail(@RequestBody EmailWrapper wrapper, Model model) {
List<String> ids = wrapper.getBemsids();
for (String s : ids) {
EmailEntity emailEntity = emailRepository.findByOwnerBemsid(s);
emailRepository.delete(emailEntity);
}
model.addAttribute("category", "admin");
model.addAttribute("subCategory", "email");
return "pages/index";
}
@RequestMapping(
value=“/admin/delete email”,
method=RequestMethod.POST,
consumes=“application/json”)
公共字符串deleteEmail(@RequestBody-EmailWrapper,Model-Model){
List id=wrapper.getBemsids();
用于(字符串s:id){
EmailEntity EmailEntity=emailRepository.findByOwnerBemsid;
emailRepository.delete(emailEntity);
}
model.addAttribute(“类别”、“管理”);
model.addAttribute(“子类别”、“电子邮件”);
返回“页面/索引”;
}
调试-断点:
注释requestbody用于restful应用程序和web mvc的modelattribute。您将两者混合使用,这就是问题所在。您是否可以尝试使用modelattribute更改requestbody,并使用表单数据代替xhr请求从前端发送数据。感谢您的回复,不幸的是,在密钥周围添加引号似乎没有帮助。我的控制器中仍然有一个空的EmailWrapper。以下是要发送到后端的json数据:{“bemsids”:[“3129426”,“240540”]}您正在使用的注释是什么,controller还是restcontroller控制器类使用控制器注释。该方法使用RequestMapping注释,方法设置为RequestMethod.POSTrequestbody用于restful应用程序和web mvc的modelattribute。您将两者混合使用,这就是问题所在。您可以尝试使用modelattributeThank更改请求正文吗。我在前端修改了dto,使其成为FormData对象,并用ModelAttribute替换了RequestBody注释,现在它似乎对我有效。如果你修改你的答案,我会接受的。谢谢