Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that SpringBoot cannot read the InputStream in the request request

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to solve the problem that SpringBoot can not read the InputStream in the request request". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to solve the problem that SpringBoot cannot read the InputStream in the request request".

Phenomenon

Using the SpringBoot2.0 version of the development project, previously found that when getting parameters, request.getParameter () can not get the parameters, without digging into it, using

@ PostMapping ("/ mbr/detail") public Result mbrDetail (@ RequestBody JSONObject jsonObject) {MbrMember mbrMember = null; try {String mbrId = jsonObject.getString ("mbrId"); mbrMember = mbrMemberService.getMbrMemberById (mbrId);} catch (Exception e) {e.printStackTrace (); return ResultGenerator.genExcepResult ("server internal error") } return ResultGenerator.genSuccessResult (mbrMember);}

@ RequestBody JSONObject jsonObject to get the parameters in the request.

Today, when we modify the original interface, it is found that no parameters can be obtained from the request request. The original interface (which does not use Spring mode) is implemented in JSP form, as follows:

Try {System.out.println ("!! enter callback!") ; String postdata = AppServiceUtil.getPostData (request.getInputStream (), request.getContentLength (), Charsets.UTF_8.toString ()); BdNotifyApi notifyApi = new BdNotifyApi (); result = notifyApi.bdNotify (postdata);} catch (Exception e) {e.printStackTrace ();} finally {response.setCharacterEncoding ("utf8"); response.setHeader ("content-type", "text/html;charset=UTF-8") Response.getWriter () .print (result);}

The getPostData method is as follows:

Public static String getPostData (InputStream in, int size, String charset) {if (in! = null & & size > 0) {byte [] buf = new byte [size]; try {in.read (buf) If (charset = = null | | charset.length () = = 0) return new String (buf); else {return new String (buf, charset) }} catch (IOException e) {e.printStackTrace ();}} return null;} request can not get the parameter analysis by stream reading

When this happens, it is suspected that the input stream has already been used. Because the request input stream is not cached, it is invalid to use the stream once. Usually, methods such as getParameter () are called to trigger parsing the input stream. After checking the code, it is suspected that the underlying layer of SpringBoot has done the processing. Check the auto-assembly configuration of SpringBoot, and initialize an OrderedHiddenHttpMethodFilter in WebMvcAutoConfiguration. The filter takes effect by default. The related code is as follows:

Bean@ConditionalOnMissingBean (HiddenHttpMethodFilter.class) @ ConditionalOnProperty (prefix = "spring.mvc.hiddenmethod.filter", name = "enabled", matchIfMissing = true) public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter () {return new OrderedHiddenHttpMethodFilter ();}

OrderedHiddenHttpMethodFilter inherits OrderedHiddenHttpMethodFilter, and OrderedHiddenHttpMethodFilter inherits HiddenHttpMethodFilter. Parameters are found to be processed in the doFilterInternal () method of this class. The related code is as follows:

@ Overrideprotected void doFilterInternal (HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {HttpServletRequest requestToUse = request; if ("POST" .equals (request.getMethod ()) & & request.getAttribute (WebUtils.ERROR_EXCEPTION_ATTRIBUTE) = = null) {String paramValue = request.getParameter (this.methodParam); if (StringUtils.hasLength (paramValue)) {String method = paramValue.toUpperCase (Locale.ENGLISH) If (ALLOWED_METHODS.contains (method)) {requestToUse = new HttpMethodRequestWrapper (request, method);} filterChain.doFilter (requestToUse, response);} configuration

Reconfigure the filter in the configuration file of SpringMVC

/ * Spring MVC configuration WebMvcConfigurer * / @ Configurationpublic class WebMvcConfigurer extends WebMvcConfigurerAdapter {

The filter code is as follows:

@ Beanpublic HiddenHttpMethodFilter hiddenHttpMethodFilter () {return new OrderedHiddenHttpMethodFilter () {@ Override protected void doFilterInternal (HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {filterChain.doFilter (request, response);};} Summary

Figure out how streaming gets request parameters and how the default configuration of SpringBoot handles this situation.

Referenc

Https://blog.csdn.net/jianggujin/article/details/86644914

At this point, I believe you have a deeper understanding of "how to solve the problem that SpringBoot can not read the InputStream in request requests". 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report