spring MVC 获取请求体

spring  MVC中如何获取请求体呢?

在spring MVC中如何获取请求要素呢?

通过如下方法:

Java代码   收藏代码
  1. /** 
  2.      * Compatible with GET and POST 
  3.      *  
  4.      * @param request 
  5.      * @return : <code>String</code> 
  6.      * @throws IOException 
  7.      */  
  8.     public static String getRequestQueryStr(HttpServletRequest request,  
  9.             String charEncoding) throws IOException {  
  10.         String submitMehtod = request.getMethod();  
  11.         if (submitMehtod.equalsIgnoreCase("post")) {  
  12.             byte[] bytes = getRequestPostBytes(request);  
  13.             String charEncoding2 = request.getCharacterEncoding();// charset  
  14.             System.out.println("[getRequestQueryStr]charEncoding:"  
  15.                     + charEncoding2);  
  16.             System.out.println("[getRequestQueryStr]Content-Type:"  
  17.                     + request.getHeader(Constant2.REQUEST_HEADER_CONTENT_TYPE));  
  18.   
  19.             if(ValueWidget.isNullOrEmpty(charEncoding)){  
  20.                 charEncoding=SystemHWUtil.CHARSET_UTF;  
  21.             }  
  22.             return new String(bytes, charEncoding);  
  23.         } else {// form method :Get  
  24.             return request.getQueryString();  
  25.         }  
  26.     }  

 对GET请求的参数是没有问题的,但是对于POST请求获取不到为什么呢?

因为spring MVC已经获取了一遍request的stream,所以再次获取时是为空的,(这是输入流的特性,可以查查官网API),所以我获取到request 的InputStream之后调用reset,但是报错,不支持reset操作.

怎么办呢?

在spring MVC 的控制器中使用@RequestBody 注解,作用是获取请求体,格式是字节数组,见代码

Java代码   收藏代码
  1. @ResponseBody  
  2.     @RequestMapping(value = "/jsonStub", produces = SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF)  
  3.     public String json(Model model, HttpSession session,@RequestBody byte[]bytes,  
  4.             HttpServletRequest request, String callback) throws IOException {  
  5.         Map map=WebServletUtil.parseRequest(request, null);  
  6.         if(ValueWidget.isNullOrEmpty(map)){  
  7.             String postStr=new String(bytes,SystemHWUtil.CURR_ENCODING);//username=huangwei&password=123  
  8.             System.out.println("postStr:"+postStr);//username=%E9%BB%84%E5%A8%81&password=123  
  9.             postStr=URLDecoder.decode(postStr,SystemHWUtil.CURR_ENCODING);//{"username":"黄威","password":"123"}  
  10.             map=WebServletUtil.parseRequestStr(postStr, true);  
  11.         }  
  12.         String content = HWUtils.getJsonP(map, callback);  
  13.         return content;  
  14.     }  

 

@RequestBody后紧跟的参数将被注入请求体的字节数组(spring mVC帮我们完成的).

注意:request.getInputStream() 读取一次之后,再次读取就读取不到

上述代码依赖的方法:

Java代码   收藏代码
  1. /*** 
  2.      * Get request query string, form method : post 
  3.      *  
  4.      * @param request 
  5.      * @return byte[] 
  6.      * @throws IOException 
  7.      */  
  8.     public static byte[] getRequestPostBytes(HttpServletRequest request)  
  9.             throws IOException {  
  10.         int contentLength = request.getContentLength();  
  11.         /*当无请求参数时,request.getContentLength()返回-1 */  
  12.         if(contentLength<0){  
  13.             return null;  
  14.         }  
  15.         byte buffer[] = new byte[contentLength];  
  16.         for (int i = 0; i < contentLength;) {  
  17.   
  18.             int readlen = request.getInputStream().read(buffer, i,  
  19.                     contentLength - i);  
  20.             if (readlen == -1) {  
  21.                 break;  
  22.             }  
  23.             i += readlen;  
  24.         }  
  25.         return buffer;  
  26.     }  
  27.   
  28.     /*** 
  29.      * Get request query string, form method : post 
  30.      *  
  31.      * @param request 
  32.      * @return 
  33.      * @throws IOException 
  34.      */  
  35.     public static String getRequestPostStr(HttpServletRequest request)  
  36.             throws IOException {  
  37.         byte buffer[] = getRequestPostBytes(request);  
  38.         String charEncoding = request.getCharacterEncoding();  
  39.         if (charEncoding == null) {  
  40.             charEncoding = "UTF-8";  
  41.         }  
  42.         return new String(buffer, charEncoding);  
  43.     }  

 

参考:http://json20080301.iteye.com/blog/1874074?utm_source=tuicool

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值