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

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

Spring / Angular 7 POST method requestparameter is null

问题

Angular:

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

Spring backend:

  1. @RequestMapping(value = "/api/example", method = RequestMethod.POST)
  2. public @ResponseBody void postExample(@RequestBody String mydata, HttpServletRequest request, HttpServletResponse response)
  3. throws IOException {
  4. HttpSession session = request.getSession();
  5. response.setContentType("application/json");
  6. response.setCharacterEncoding("UTF-8");
  7. request.setCharacterEncoding("UTF-8");
  8. System.out.println(mydata);
  9. // ...
  10. }

Edit:
If you want to use @RequestBody:

  1. @RequestMapping(value = "/api/example", method = RequestMethod.POST)
  2. public @ResponseBody void postExample(@RequestBody String[][] mydata, HttpServletRequest request, HttpServletResponse response)
  3. throws IOException {
  4. HttpSession session = request.getSession();
  5. response.setContentType("application/json");
  6. response.setCharacterEncoding("UTF-8");
  7. request.setCharacterEncoding("UTF-8");
  8. System.out.println(Arrays.deepToString(mydata));
  9. // ...
  10. }

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:

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

JSON.stringify(data) looks like this:

  1. [
  2. ["num1","num2","num3","num4","num5", "num6"],
  3. ["test6","test2","test1","test5","test4", "test3"]
  4. ]

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

Spring backend:

  1. @RequestMapping(value = "/api/example", method = RequestMethod.POST)
  2. public @ResponseBody void postExample(HttpServletRequest request, HttpServletResponse response)
  3. throws IOException {
  4. HttpSession session = request.getSession();
  5. response.setContentType("text/html");
  6. response.setCharacterEncoding("UTF-8");
  7. request.setCharacterEncoding("UTF-8");
  8. String mydata = request.getParameter("mydata");
  9. System.out.println(mydata);
  10. ...
  11. }

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数据结构,例如一个嵌套数组

  1. @PostMapping(path="/api/example")
  2. public void postExample(@RequestBody ArrayList<ArrayList<String>> body) {
  3. //....
  4. }

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

  1. [
  2. ["num1","num2","num3","num4","num5", "num6"],
  3. ["test6","test2","test1","test5","test4", "test3"]
  4. ]
英文:

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

  1. @PostMapping(path=&quot;/api/example&quot;)
  2. public void postExample(@RequestBody ArrayList&lt;ArrayList&lt;String&gt;&gt; body) {
  3. //....
  4. }

Test case (from question above)

  1. [
  2. [&quot;num1&quot;,&quot;num2&quot;,&quot;num3&quot;,&quot;num4&quot;,&quot;num5&quot;, &quot;num6&quot;],
  3. [&quot;test6&quot;,&quot;test2&quot;,&quot;test1&quot;,&quot;test5&quot;,&quot;test4&quot;, &quot;test3&quot;]
  4. ]

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:

确定