请求参数如果放在HttpPost或者HttpGet中后端该如何取参?
warning:
这篇文章距离上次修改已过419天,其中的内容可能已经有所变动。
如果我们把请求参数如果放在HttpPost或者HttpGet中后端该如何取参?
有两种方式
一种是从请求数据流中获取数据
@AnonymousPostMapping("/api/router")
public RestResult router(HttpServletRequest requestEntity) throws Exception {
String inputStream = getInputStream(requestEntity);
// Object requestEntityBody = requestEntity.getBody();
System.out.println(123);
return RestResult.success();
}
<!--more-->
public static String getInputStream(HttpServletRequest request) throws Exception {
ServletInputStream stream = null;
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
try {
stream = request.getInputStream();
// 获取响应
reader = new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
// logger.error(e);
} finally {
reader.close();
}
return sb.toString();
}
方式二:
如果用的是springboot框架
可以用自带的 RequestEntity 作为实体进行接收
包是:package org.springframework.http;
@AnonymousPostMapping("/api/router")
public RestResult router(RequestEntity requestEntity) throws IOException {
Object requestEntityBody = requestEntity.getBody();
System.out.println(123);
return RestResult.success();
}
虽然看不懂,看着像是JAVA。哈哈要不要换个友链呢?我的小站是:http://www.edtmpsna.com/