1. 程式人生 > >接收httpPost請求HttpEntity方式

接收httpPost請求HttpEntity方式

使用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));
        }
    }


}