开发者

如何从request中获取body的数据

开发者 https://www.devze.com 2023-11-20 10:19 出处:网络 作者: 月未明
目录从request中获取body的数据说下场景总结从request中获取body的数据 如果在controller里直接@RequestBody就可以获取,这种方式很简单,现在说下直接从request中获取。
目录
  • 从request中获取body的数据
  • 说下场景
  • 总结

从request中获取body的数据

如果在controller里直接@RequestBody就可以获取,这种方式很简单,现在说下直接从request中获取。

说下场景

我是在shiro的filter中获取body中的数据:

@Overridjavascripte
    public boolean onAccessDenied(ServletRequest servletRequest, ServletResponse response) throws Exception{
        log.info("into onAccessDenied");
        try {
            HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;

            String str = httpServletRequest.getQueryString();
            BufferedReader bufferedReader = httpServletRequest.getReader();
            String bodyStr = IOUtils.read(bufferedReader);
            System.out.println("bodyStr = " + bodyStr www.devze.com);
            return true;
        }catch (ExpiredCredentialsException e){
            log.info("请求信息过期");
            WebUtils.toHttp(response).sendError(401,"请求信息过期,操作失败");
        }catch (Exception e){
            log.info("请求参数不合法");
            e.printStackTrace();
            WebUtils.toHttp(response).sendError(401,e.getMessage());
        }

        return false;
    }

这里给出主要的相关代码,IOUtils是dubbo的一个类

相关依赖如下:

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>

通过这种方式获取的是一个json字符串,

bodyStr =  {
    "timestamp":1523966342156
}

因为我项目中没有用到dubbo,如果为了一个body的解析引入这个感觉有些不太合适,而且启动的时候也没有正常启动,所以我把这块代码抽取了出来:

package com.test.shiro.util;

import Java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

/**
 * @author Created by pangkunkun on 2018/4/20.
 * 这段代码来自dubbo
 *
 */
public class RequestBodyUtils {

    private static final int BUFFER_SIZE = 1024 * 8;

    /**
     * read string.
     *
     * @param reader Reader instance.
     * @return String.
     * @throws IOException
     */
    public static String read(Reader reader) throws IOException
    {
        StringWriter writer = new StringWriter();
        try
        {
            write(reader, writer);
            return androidwriter.getBuffer().toString();
        }
        finally{ writer.close(); }
    }

    /**
     * write.
     *
     * @param reader Reader.
     * @param writer Writer.
     * @return count.
     * @throws IOException
     */
    public static long write(Reader reader, Writer writer) throws IOException
    {
        return write(reader, writer, BUFFER_SIZE);
    }

    /**
     * write.
     *
     * @param reader Reader.
     * @param writer Writer.
     * @param bufferSize buffer size.
     * @return count.
     * @throws IOException
     */
    public static long write(Reader reader, Writer writer, int bufpythonferSize) throws IOExpythonception
    {
        int read;
        long total = 0;
        char[] buf = new char[BUFFER_SIZE];
        while( ( read = reader.read(buf) ) != -1 )
        {
            writer.write(buf, 0, read);
            total += read;
        }
        return total;
    }

}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号