Spring Interceptor gets JSON-formatted data from request

Source: Internet
Author: User

Content Title

1. Create a value class

1 /**2 * Myrequestwrapper.java3  * 4 * @screen5  * @authorHavery6  */7  PackageCom.newpp.core.interceptor;8 9 ImportJava.io.BufferedReader;Ten ImportJava.io.ByteArrayInputStream; One Importjava.io.IOException; A ImportJava.io.InputStream; - ImportJava.io.InputStreamReader; -  the ImportJavax.servlet.ServletInputStream; - Importjavax.servlet.http.HttpServletRequest; - ImportJavax.servlet.http.HttpServletRequestWrapper; -  + /** - * Myrequestwrapper. +  *  A  * @authorHavery at  */ -  Public classMyrequestwrapperextendsHttpservletrequestwrapper { -     Private FinalString body; -  -      PublicMyrequestwrapper (HttpServletRequest request)throwsIOException { -         Super(request); inStringBuilder StringBuilder =NewStringBuilder (); -BufferedReader BufferedReader =NULL; to         Try { +InputStream InputStream =Request.getinputstream (); -             if(InputStream! =NULL) { theBufferedReader =NewBufferedReader (NewInputStreamReader (InputStream)); *                 Char[] Charbuffer =New Char[128]; $                 intBytesread =-1;Panax Notoginseng                  while((Bytesread = Bufferedreader.read (charbuffer)) > 0) { -Stringbuilder.append (charbuffer, 0, bytesread); the                 } +}Else { AStringbuilder.append (""); the             } +}Catch(IOException ex) { -             Throwex; $}finally { $             if(BufferedReader! =NULL) { -                 Try { - bufferedreader.close (); the}Catch(IOException ex) { -                     Throwex;Wuyi                 } the             } -         } WuBODY =stringbuilder.tostring (); -     } About  $ @Override -      PublicServletInputStream getInputStream ()throwsIOException { -         FinalBytearrayinputstream Bytearrayinputstream =NewBytearrayinputstream (Body.getbytes ()); -ServletInputStream ServletInputStream =NewServletInputStream () { A              Public intRead ()throwsIOException { +                 returnBytearrayinputstream.read (); the             } -         }; $         returnServletInputStream; the     } the  the @Override the      PublicBufferedReader Getreader ()throwsIOException { -         return NewBufferedReader (NewInputStreamReader ( This. getInputStream ())); in     } the  the      PublicString GetBody () { About         return  This. Body; the     } the}


2. Take the value

New Myrequestwrapper ((httpservletrequest) request);         = Myrequestwrapper.getbody ();

Spring Interceptor gets JSON-formatted data from request

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.