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

warning: 这篇文章距离上次修改已过247天,其中的内容可能已经有所变动。

如果我们把请求参数如果放在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();
    }
版权声明 ▶ 本网站名称:我的学习笔记
▶ 本文链接:https://ooolo.net/article/165.html
▶ 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行核实删除。
▶ 转载本站文章需要遵守:商业转载请联系站长,非商业转载请注明出处!!!

none
最后修改于:2023年07月25日 15:41

仅有一条评论

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

添加新评论

icon_mrgreen.pngicon_neutral.pngicon_twisted.pngicon_arrow.pngicon_eek.pngicon_smile.pngicon_confused.pngicon_cool.pngicon_evil.pngicon_biggrin.pngicon_idea.pngicon_redface.pngicon_razz.pngicon_rolleyes.pngicon_wink.pngicon_cry.pngicon_surprised.pngicon_lol.pngicon_mad.pngicon_sad.pngicon_exclaim.pngicon_question.png