0

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?

Shephard
  • 117
  • 1
  • 14
  • 1
    The problem is that `mydata` isn't a request parameter, but a request body. The easiest way is to use `@RequestBody` but if you don't like it, you have to use one of the following approaches: https://stackoverflow.com/questions/8100634/get-the-post-request-body-from-httpservletrequest – sergiomse Aug 26 '20 at 07:52
  • thank you. IF I would like to use @RequestBody, how can I use it, in this case? (because as you can see, I have multiple arrays) – Shephard Aug 26 '20 at 08:05
  • 1
    You have to model your JSON schema as Java objects with Lists, as Marc says: https://stackoverflow.com/a/63595324/2470255 – sergiomse Aug 26 '20 at 10:16

2 Answers2

1

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

Vladimir Stanciu
  • 1,468
  • 1
  • 7
  • 24
1

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

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

    //....

}

Test case (from question above)

[
  ["num1","num2","num3","num4","num5", "num6"],
  ["test6","test2","test1","test5","test4", "test3"]
]
Marc Stroebel
  • 2,295
  • 1
  • 12
  • 21
  • 1
    I tried this but I found a problem, when I print out `JSON.stringify(data)` in angular, then we can see the correct json (see above), but when I use `@RequestBody String mydata`, then I will get this:`{"mydata":"[[\"num1\",\"num2\",\"num3\",\"num4\",\"num5\",\"num6\"],[\"test6\",\"test2\",\"test1\",\"test5\",\"test4\",\"test3\"]]"}` after that if I change String to ArrayList>, I will get `JSON parse error: Cannot deserialize instance of java.util.ArrayList> out of START_OBJECT token;` error. – Shephard Aug 26 '20 at 11:58
  • If I am correct, then I should use simple String and parse everything by myself? – Shephard Aug 26 '20 at 11:59
  • 1
    no, then use a pojo: `public class Dto { @JsonProperty("mydata") public ArrayList> myData; }` – Marc Stroebel Aug 26 '20 at 12:06
  • I tried as you wrote + I had to change `this.http.post('api/example', { mydata: JSON.stringify(data) }` to `this.http.post('api/example', { mydata: data }` (removed JSON.stringify) and now it is working, thank you. – Shephard Aug 26 '20 at 15:40