Get the App
SLTechnology News&Howtos  ›  Development  › 

SpringBoot prints the process of requesting the original input parameter body by POST

Shulou Source: shulou.com Published: 2022-06-03 15:31:47 10月12日 Update

The main content of this article is to explain "the process of SpringBoot printing POST to request the original input body". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the process of SpringBoot printing POST to request the original input Body body.

Catalogue

SpringBoot print POST request original input body

1. Define the filter configuration first

2. Implement the filter in 1

Post cannot receive parameters in body (object parameters)

Check the comments

Check entity

Check Content-Type

SpringBoot prints the original input parameter body1 of the POST request. First define the filter configuration package com.choice.o2o.device.common.config; import com.choice.o2o.device.common.filter.LogFilter;import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration; @ Configurationpublic class FilterConfig {@ Bean public FilterRegistrationBean registFilter () {FilterRegistrationBean registration = new FilterRegistrationBean (); registration.setFilter (new LogFilter () Registration.addUrlPatterns ("/ *"); registration.setName ("LogFilter"); registration.setOrder (1); return registration;}} 2, filter package com.choice.o2o.three.code.config.log; import lombok.extern.slf4j.Slf4j;import org.springframework.core.Ordered;import org.springframework.web.filter.OncePerRequestFilter;import org.springframework.web.util.ContentCachingRequestWrapper;import org.springframework.web.util.ContentCachingResponseWrapper in implementation 1 Import org.springframework.web.util.WebUtils; import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.Enumeration; @ Slf4jpublic class LogParamFilter extends OncePerRequestFilter implements Ordered {/ / put filter at the end of all other filters to make sure we are processing after all others private int order = Ordered.LOWEST_PRECEDENCE-8; public static final String SPLIT_STRING_M = "=" Public static final String SPLIT_STRING_DOT = ","; @ Override public int getOrder () {return order;} @ Override protected void doFilterInternal (HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {ContentCachingRequestWrapper wrapperRequest = new ContentCachingRequestWrapper (request); ContentCachingResponseWrapper wrapperResponse = new ContentCachingResponseWrapper (response); String urlParams = getRequestParams (request) FilterChain.doFilter (wrapperRequest, wrapperResponse); String requestBodyStr = getRequestBody (wrapperRequest); log.info ("params [{}] | request body: {}", urlParams, requestBodyStr); String responseBodyStr = getResponseBody (wrapperResponse); log.info ("response body: {}", responseBodyStr); wrapperResponse.copyBodyToResponse () } / * * print request parameters * * @ param request * / private String getRequestBody (ContentCachingRequestWrapper request) {ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest (request, ContentCachingRequestWrapper.class); if (wrapper! = null) {byte [] buf = wrapper.getContentAsByteArray (); if (buf.length > 0) {String payload Try {payload = new String (buf, 0, buf.length, wrapper.getCharacterEncoding ());} catch (UnsupportedEncodingException e) {payload = "[unknown]";} return payload.replaceAll ("\\ n", ");}} return" } / * print return parameter * * @ param response * / private String getResponseBody (ContentCachingResponseWrapper response) {ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse (response, ContentCachingResponseWrapper.class); if (wrapper! = null) {byte [] buf = wrapper.getContentAsByteArray (); if (buf.length > 0) {String payload Try {payload = new String (buf, 0, buf.length, wrapper.getCharacterEncoding ());} catch (UnsupportedEncodingException e) {payload = "[unknown]";} return payload;}} return "" } / * get the parameter on the request address * * @ param request * @ return * / public static String getRequestParams (HttpServletRequest request) {StringBuilder sb = new StringBuilder (); Enumeration enu = request.getParameterNames (); / / get the request parameter while (enu.hasMoreElements ()) {String name = enu.nextElement () Sb.append (name + SPLIT_STRING_M) .append (request.getParameter (name)); if (enu.hasMoreElements ()) {sb.append (SPLIT_STRING_DOT);}} return sb.toString ();}} Post cannot receive the parameter (object parameter) check comment in body

@ ResponseBody

@ RequestBody

Check entity

Whether the set and get methods are correct for receiving entity classes

Check Content-Type

Is it application/json?

At this point, I believe you have a deeper understanding of the process of "SpringBoot printing POST request for the original input bodybody". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Parameters checks primitive filters procedures entities contents objects methods annotations learning configuration practicality deeper interest address practicality practicality simplicity of operation more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MySQL Shulou Technology Huawei Linux