Java接口样例(使用注解方式)

@RestController
@Transactional
public class CmsInterfaceController extends BaseController {
    private static final Log LOGGER = LogFactory.getLog("interfaceLogFile");


    @RequestMapping("/cmsInterface/upateSecurityKey")
    public void upateSecurityKey(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        InputStream is = null;
        StringBuilder strBuffer = new StringBuilder();
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        String timeStamp = format;
        timeStamp = timeStamp.replace(" ", "T");
        timeStamp = timeStamp + ".0Z";
        try {
            is = request.getInputStream();
            // 读取request内容
            ServletInputStream sis = (ServletInputStream) is;
            byte[] lineByte = new byte[Constants.NUMBER_1024];
            int readResult = 0;
            while ((readResult = sis.readLine(lineByte, 0, lineByte.length)) != -1) {
                strBuffer.append(new String(lineByte, 0, readResult, "utf-8"));
            }
            LOGGER.info(strBuffer.toString());
            // 刷新request流
            ByteArrayInputStream bis = new ByteArrayInputStream(
                    strBuffer.toString().getBytes("utf-8"));
            // 解析request流,业务操作
            StringBuilder returnStr = appendXml(timeStamp,Constants.NUMBER_0);
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(returnStr.toString().getBytes());
            LOGGER.info("成功");
        } catch (Exception e) {
            StringBuilder returnStr = appendXml(timeStamp,Constants.NUMBER_1);
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(returnStr.toString().getBytes());
          
        }
    }


private StringBuilder appendXml(String timeStamp,Integer code) {
    StringBuilder returnStr = new StringBuilder(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    returnStr.append("<message>");
    returnStr.append("<header transactionID=\"固定值\" timeStamp=\""+ timeStamp
                    + "\" accessToken =\"\" commandType=\"KeyDistributeRes\"/>");
    returnStr.append("<body>");
    returnStr.append("<KeyDistributeRes errorCode=\"").append(code).append("\" errorDescription=\"\"/>");
    returnStr.append("</body>");
    returnStr.append("</message>");
    return returnStr;
}
@RequestMapping(value = "/authreadepg", method = { RequestMethod.POST })
@ResponseBody
public void readEpgInput(HttpServletRequest request,HttpServletResponse response) throws Exception {
    LOGGER.info("request流,收到来自" + request.getRemoteHost() + "的请求。");
    InputStream in = request.getInputStream();
    String code = Constants.RESULT_CODE_SUCCESS;
    String description = "";
    try {
        String ret =cmsInterfaceService.readEpgInput(in);
        if(Constants.RET_OK_0.equals(ret)){
            sendEpgChangeMessage();
        }else{
            code = Constants.RESULT_CODE_FAILED;
            description = Constants.RESULT_DESCRIPTION;
        }
    } catch(Exception ex){
        code = Constants.RESULT_CODE_FAILED;
        description = Constants.RESULT_DESCRIPTION;
        LOGGER.info(Constants.RESULT_DESCRIPTION);
    }finally{
        try{
            if(in != null){
                in.close();
            }
        }catch(Exception e){
            LOGGER.info("关闭流文件异常:" + e.getMessage());
        }
    }
    
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/xml");
    StringBuffer  sb = new StringBuffer();
    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("\n")
    .append("<message>").append("\n").append("<header action=\"RESPONSE\" command=\"READ_EPG_INFO\"/>").append("\n")
    .append("<body>").append("\n").append("<result code=\"").append(code).append("\" description=\"")
    .append(description).append("\"/>").append("\n")
    .append("</body>").append("\n").append("</message>");
    LOGGER.info(sb.toString());
    response.getWriter().write(sb.toString());
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值