• 首页
  • 邻居
  • 关于
  • 归档
  • 搜索
  • 夜间模式
    ©2020-2026  我的学习笔记 Theme by OneBlog

    我的学习笔记博客

    搜索
    标签
    # 随笔 # Java # 教程 # openwrt # Mysql # SQL # 爬虫 # post # Js调优 # MAVEN
  • 首页>
  • Java>
  • 正文
  • 请求参数如果放在HttpPost或者HttpGet中后端该如何取参?

    2023年03月09日 2.2 k 阅读 1 评论 1495 字

    如果我们把请求参数如果放在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();
        }
    本文著作权归作者 [ admin ] 享有,未经作者书面授权,禁止转载,封面图片来源于 [ 互联网 ] ,本文仅供个人学习、研究和欣赏使用。如有异议,请联系博主及时处理。
    取消回复

    发表留言
    回复

    读者留言1

    1. newdomer Lv.1
      2023-03-11 10:55 回复

      虽然看不懂,看着像是JAVA。哈哈要不要换个友链呢?我的小站是:http://www.edtmpsna.com/

    加载更多评论
    加载中...
    — 已加载全部评论 —
    首页邻居关于归档
    Copyright©2020-2026  All Rights Reserved.  Load:0.017 s
    京ICP备18019712号
    Theme by OneBlog V3.6.5
    夜间模式

    开源不易,请尊重作者版权,保留基本的版权信息。