英文:
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="/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"]
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论