春季 / Angular 7 的 POST 方法请求参数为空。

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

Spring / Angular 7 POST method requestparameter is null

问题

Angular:

this.http.post('api/example', JSON.stringify(data), 
{ "headers": { "Content-Type": "application/json; charset=UTF-8" } }).subscribe(Response => console.log(Response));

Spring backend:

@RequestMapping(value = "/api/example", method = RequestMethod.POST)
public @ResponseBody void postExample(@RequestBody String mydata, HttpServletRequest request, HttpServletResponse response)
        throws IOException {

    HttpSession session = request.getSession();

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    request.setCharacterEncoding("UTF-8");

    System.out.println(mydata);
    // ...
}

Edit:
If you want to use @RequestBody:

@RequestMapping(value = "/api/example", method = RequestMethod.POST)
public @ResponseBody void postExample(@RequestBody String[][] mydata, HttpServletRequest request, HttpServletResponse response)
        throws IOException {

    HttpSession session = request.getSession();

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    request.setCharacterEncoding("UTF-8");

    System.out.println(Arrays.deepToString(mydata));
    // ...
}

Note: The above code assumes you've imported the necessary libraries and set up your project correctly.

英文:

I would like to send datas (in json form) from frontend to backend using POST, but requestparameter is null.

Angular:

this.http.post('api/example', { mydata: JSON.stringify(data) },
 { "headers": { header: "text/html;charset=UTF-8" } }).subscribe(Response => console.log(Response);
}); 

JSON.stringify(data) looks like this:

[
  ["num1","num2","num3","num4","num5", "num6"],
  ["test6","test2","test1","test5","test4", "test3"]
]

This is just an example, the data will be dynamic, so sometimes I will have more or less columns and rows.

Spring backend:

@RequestMapping(value = "/api/example", method = RequestMethod.POST)
public @ResponseBody void postExample(HttpServletRequest request, HttpServletResponse response)
        throws IOException {

    HttpSession session = request.getSession();

    response.setContentType("text/html");
    response.setCharacterEncoding("UTF-8");
    request.setCharacterEncoding("UTF-8");

    String mydata = request.getParameter("mydata");
    System.out.println(mydata);
    ...
}

mydata is null, when I print out. I don't know why.
What I tried:

  • change "text/html" to "application/json" and "application/*" and "text/plain" (and wanted to convert the text to json at backend, but still null parameter)

I would like to use "getParameter" instead of using @RequestBody annotation.

How can I get the json data from frontend and use it in backend?

Edit:
Originally I didn't want to use @RequestBody, but if I want to use it, how can I use it for getting these json arrays?

答案1

得分: 1

你需要使用 .getReader() 方法,而不是 .getParameter() 方法,因为你需要获取请求的主体,而不是某个参数。

英文:

You need to use .getReader(), instead of the .getParameter() method, since you need to retrieve the body of the request not some parameter.

答案2

得分: 1

使用 @RequestBody,您将需要一个与您的JSON匹配的Java数据结构,例如一个嵌套数组

@PostMapping(path="/api/example")
public void postExample(@RequestBody ArrayList<ArrayList<String>> body) {

    //....

}

测试用例(来自上面的问题)

[
  ["num1","num2","num3","num4","num5", "num6"],
  ["test6","test2","test1","test5","test4", "test3"]
]
英文:

for using @RequestBody you'll need a Java data structure matching your JSON,
e.g. a nested array

@PostMapping(path=&quot;/api/example&quot;)
public void postExample(@RequestBody ArrayList&lt;ArrayList&lt;String&gt;&gt; body) {

    //....

}

Test case (from question above)

[
  [&quot;num1&quot;,&quot;num2&quot;,&quot;num3&quot;,&quot;num4&quot;,&quot;num5&quot;, &quot;num6&quot;],
  [&quot;test6&quot;,&quot;test2&quot;,&quot;test1&quot;,&quot;test5&quot;,&quot;test4&quot;, &quot;test3&quot;]
]

huangapple
  • 本文由 发表于 2020年8月26日 15:40:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63592808.html
匿名

发表评论

匿名网友

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

确定