一:前端传来的json (Postman的POST提交方式中的raw提交),用以下两种方式接收:
1、io流的方式
@RequestMapping("/readRaw")
@ResponseBody
public String readRaw(InputStream inputStream) {
String result = "";
try {
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1)
{
outSteam.write(buffer, 0, len);
}
outSteam.close();
inputStream.close();
result = new String(outSteam.toByteArray(), "UTF-8");
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
2、用注解的方式
@RequestMapping(value="/test", method = RequestMethod.POST)
@ResponseBody
public String test(HttpServletRequest request,HttpServletResponse response,@RequestBody String str){
System.out.println(str);
return str;
}
二:前端传来的值(Postman的POST提交方式中的form-data提交)文章来源:https://www.toymoban.com/news/detail-562516.html
三:前端传来的值(Postman的POST提交方式中的x-www-form-urlencoded提交)一样的文章来源地址https://www.toymoban.com/news/detail-562516.html
用String name=request.getParameter("name");接收
@RequestMapping("/test1")
@ResponseBody
public String test1(HttpServletRequest request,HttpServletResponse response){
String name=request.getParameter("name");
System.out.println(name);
return name;
}
到了这里,关于JSON请求数据如何转换为bean对象的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!