您的位置:首页 > 编程语言 > Java开发

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

2017-10-19 09:32 411 查看
@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());

}

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