您的位置:首页 > 理论基础 > 计算机网络

接收httpPost请求HttpEntity方式

2015-12-25 15:47 701 查看
使用org.springframework.http.HttpEntity 接收数据,方便省事,不用再用流去解析数据,直接看示例:

public void callback(HttpEntity<byte[]> requestEntity, HttpServletResponse response) {

        String txNo =  UUID.randomUUID();

        try{ 

//直接解析

            byte[] body = requestEntity.getBody();

      获取数据

            String requestJson = new String(body,"UTF-8");

           

            log.info("callback,txNo:{},requestJson :{}",txNo,requestJson);

            

            log.info("fengWo callback  decode,txNo:{},requestJson :{}",txNo,URLDecoder.decode(requestJson, "UTF-8"));

//业务代码 不需细看

            FengWoCallBack callback = JSON.parseObject(requestJson, FengWoCallBack.class);

           

            output(response, "1");

                  

            } 
log.info("fengWo callback finished,txNo:{} ,dto:{}", txNo, activeOrderDTO);

        } catch (Exception e){

        output(response, "1");

            log.error("fengWo exception :{} ,teNo{}", ExceptionUtils.getStackTrace(e),txNo);

        }

    }

    

//返回

    private void output(HttpServletResponse response, String content) {

        try {

            response.getOutputStream().write(content.getBytes());

            response.getOutputStream().flush();

        } catch (Exception e) {

        log.error("fengWo exception :{}", ExceptionUtils.getStackTrace(e));

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息