View data for HTTP requests.

We may encounter that MVC cannot get the front-end parameters. We don’t know which link has the problem during the investigation, but generally confirm whether the parameters are carried in the http request. The author will introduce how to obtain the request. data in

If it is a GET request

If it is a GET request, it is very simple. The complete data is in the uri, and you can easily find it through the interface provided by servletthe specification . For example, you can do the following:

// 打一个断点在 Spring MVC 的 DispatcherServlet 类的 doDispatch 方法上,在 IDEA 的 debug 窗口中执行
request.getParameterMap()   // 查看所有参数

If it is a POST request

If it is a POST request, it will be a little troublesome, because its data is stored in the stream, which is not convenient to view directly. Usually there are two methods

Method 1: DEBUG window ( Shuang, Super Shuang, Wu Dishuang ):

The steps to view are as follows:

1. Make a breakpoint on the doDispatch method of the DispatcherServlet class of Spring MVC

2. The data of the body in the post request is stored in this location:((Http11InputBuffer) ((RequestFacade) request).request.coyoteRequest.inputBuffer).byteBuffer.hb

3. Then convert the above byte array into a string. Execute it with the debug window of IDEA

4. The complete command is as follows:

// 查看 post 请求中 body 数据的
new String(((Http11InputBuffer) ((RequestFacade) request).request.coyoteRequest.inputBuffer).byteBuffer.hb)

It's very simple. Readers can do it by themselves: check the data of the request body and confirm whether there are any parameters in the request.

5. Then you can locate the problem conveniently and quickly.

Method 2: Write method to read data in the stream (complex and difficult to use):

Note: The three methods are conflicting and can only be read once. Repeated reading will report java.io.IOException: Stream closed exception

Write a utility class to read stream data. The tool code is as follows:

package com.firefish.pretty.handler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

public class HttpServletRequestReader {
    
    
	// 字符串读取
	// 方法一
	public static String readBodyAsChars(HttpServletRequest request) {
    
    

		BufferedReader br = null;
		StringBuilder sb = new StringBuilder("");
		try {
    
    
			br = request.getReader();
			String str;
			while ((str = br.readLine()) != null) {
    
    
				sb.append(str);
			}
			br.close();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (null != br) {
    
    
				try {
    
    
					br.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}
		return sb.toString();
	}

	// 方法二
	public static void readBodyAsChars2(HttpServletRequest request) {
    
    
		InputStream is = null;
		try {
    
    
			is = request.getInputStream();
			StringBuilder sb = new StringBuilder();
			byte[] b = new byte[4096];
			for (int n; (n = is.read(b)) != -1; ) {
    
    
				sb.append(new String(b, 0, n));
			}
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (null != is) {
    
    
				try {
    
    
					is.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}

	}

	// 二进制读取
	public static byte[] readBodyAsBytes(HttpServletRequest request) {
    
    

		int len = request.getContentLength();
		byte[] buffer = new byte[len];
		ServletInputStream in = null;

		try {
    
    
			in = request.getInputStream();
			in.read(buffer, 0, len);
			in.close();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (null != in) {
    
    
				try {
    
    
					in.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}
		return buffer;
	}
}

Portal: nanny Spring5 source code analysis

Welcome to exchange technology and work life with the author

contact author

Guess you like

Origin blog.csdn.net/yuchangyuan5237/article/details/130960412