请求参数如果放在HttpPost或者HttpGet中后端该如何取参?

2023-07-17 19:50:35 浏览数 (1)

如果我们把请求参数如果放在HttpPost或者HttpGet中后端该如何取参?

有两种方式

一种是从请求数据流中获取数据 ```java @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(); } ``` ```java 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;

```java @AnonymousPostMapping("/api/router") public RestResult router(RequestEntity requestEntity) throws IOException { Object requestEntityBody = requestEntity.getBody(); System.out.println(123); return RestResult.success(); } ```

0 人点赞