将DTO提交给Spring Controller,参数为空。

huangapple go评论73阅读模式
英文:

Post DTO to Spring Controller, parameters are null

问题

Front end:

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

Wrapper:

public class EmailWrapper {
  List<String> bemsids;

  public List<String> getBemsids() {
    return bemsids;
  }

  public void setBemsids(List<String> bemsids) {
    this.bemsids = bemsids;
  }
}

Controller:

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

Debug - Breakpoint:

Breakpoint reveals getBemsids is null:
将DTO提交给Spring Controller,参数为空。

ids is null:
将DTO提交给Spring Controller,参数为空。

英文:

I am trying to Post a list of strings from my Javascript front end to a Spring Boot Controller, for some reason the post happens, but my values are null. Is there anything obviously wrong with my code?

Front end:

let bemsidList = new Array()
bemsidList[0] = &quot;3129426&quot;;
bemsidList[1] = &quot;240540&quot;;
let postData = { bemsids: bemsidList};

var xhr = new XMLHttpRequest();
xhr.open(&quot;POST&quot;, &quot;/admin/delete-email&quot;, true);
xhr.setRequestHeader(&#39;Content-Type&#39;, &#39;application/json&#39;);
xhr.send(JSON.stringify({
    postData
}));

Wrapper:

public class EmailWrapper {
  List&lt;String&gt; bemsids;

  public List&lt;String&gt; getBemsids() {
    return bemsids;
  }

  public void setBemsids(List&lt;String&gt; bemsids) {
    this.bemsids = bemsids;
  }
}

Controller:

  @RequestMapping(
  value = &quot;/admin/delete-email&quot;,
  method = RequestMethod.POST,
  consumes = &quot;application/json&quot;)
public String deleteEmail(@RequestBody EmailWrapper wrapper, Model model) {
  List&lt;String&gt; ids = wrapper.getBemsids();
  for (String s : ids) {
    EmailEntity emailEntity = emailRepository.findByOwnerBemsid(s);
    emailRepository.delete(emailEntity);
  }

  model.addAttribute(&quot;category&quot;, &quot;admin&quot;);
  model.addAttribute(&quot;subCategory&quot;, &quot;email&quot;);

  return &quot;pages/index&quot;;
}

Debug - Breakpoint:

将DTO提交给Spring Controller,参数为空。

将DTO提交给Spring Controller,参数为空。

答案1

得分: 1

Annotation @RequestBody 用于 RESTful 应用程序,@ModelAttribute 用于 Web MVC。您正在混淆这两者,这就是问题所在。您可以尝试将 @RequestBody 改为 @ModelAttribute,并且使用表单数据代替 XHR 请求从前端发送数据。

英文:

Annotation requestbody is used for restful applications & modelattribute for web mvc..you are mixing both and that is the issue here. Can you try changing requestbody with modelattribute and use form data instead xhr request to send data from frontend.

huangapple
  • 本文由 发表于 2020年9月1日 03:41:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63677184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定