In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-11-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.