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());
}
}
@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());
}
}
相关文章推荐
- hibernate4.3.10使用注解映射方式样例
- ssh使用Java注解的方式声明架构
- 【Java】【反射】 使用反射方式去实现一个接口
- 【Java】【反射】 使用反射方式去实现一个接口
- JAVA中Arrays.sort()使用两种方式(Comparable和Comparator接口)对对象或者引用进行排序
- JAVA多线程实现的三种方式(继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程)
- JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没
- JAVA中Arrays.sort()使用两种方式(Comparable和Comparator接口)对对象或者引用进行排序
- JAVAWEB开发之SpringMVC详解(一)——SpringMVC的框架原理、架构简介、与mybatis整合和注解方式的使用、
- 【Java】【反射】 使用反射方式去实现一个接口
- JAVAWEB开发之SpringMVC详解(一)——SpringMVC的框架原理、架构简介、与mybatis整合和注解方式的使用、
- java.util.LIST是个接口,不够直观,要学习ms使用IList做为接口命名的方式
- 纯java 使用spring的注解方式AOP
- 使用java 编程的注解的方式定制spring security
- 【Java】【反射】 使用反射方式去实现一个接口
- java调用WSDL接口(使用axis方式)
- JAVA中Arrays.sort()使用两种方式(Comparable和Comparator接口)对对象或者引用进行排序
- JAVA中Arrays.sort()使用两种方式(Comparable和Comparator接口)对对象或者引用进行排序
- Spring依赖注入——java项目中使用spring注解方式进行注入
- 【Java】【反射】 使用反射方式去实现一个接口