请求参数如果放在HttpPost或者HttpGet中后端该如何取参? Published on Mar 9, 2023 in Java with 1 comment 如果我们把请求参数如果放在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(); } ``` 本文由 admin 创作,采用 知识共享署名4.0 国际许可协议进行许可。本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。
虽然看不懂,看着像是JAVA。哈哈要不要换个友链呢?我的小站是:http://www.edtmpsna.com/