JAXB+Maven+ESB+Restful+XStream

编程技术  /  houtizong 发布于 3年前   94
JAXB在rest架构中充当着不可或缺的角色,是xml数据得以转化为java对象
注册JAXB类

      public void registerClasses(Class[] classes){
try {
JAXBContext context = JAXBContext.newInstance(classes);
} catch (JAXBException e) {
log.error("Initialize JAXB failed:" + e.getMessage());
}
}

XStream配合JAXB的工作,其中client端的类需要序列化,server端不需要
client 端的类定义

@XmlRootElement(name = "VirtualRoomFilter")
public class VirtualRoomFilter implements java.io.Serializable
{
public VirtualRoomFilter(){};

public String name = null;

public Integer customerId = null;

public Integer siteId = null;

public String status = null;

public String e164 = null; 

public Integer mcuId = null;

}

server 端的类定义

@XmlRootElement(name = "VirtualRoomFilter")
public class VirtualRoomFilter implements java.io.Serializable
{
public VirtualRoomFilter(){};

public String name = null;

public Integer customerId = null;

public Integer siteId = null;

public String status = null;

public String e164 = null; 

public Integer mcuId = null;

}

rest(ESB)的逻辑
@POST
@Path("/getvrsfltr/")
public String getVirtualRoomsByFilter(
@HeaderParam(SERV_NAME) String serv_name,
@HeaderParam(SERV_PW) String serv_pw,
@HeaderParam(USER_NAME) String user_name,
@HeaderParam(USER_PW)   String user_pw,
@HeaderParam(VERSION)   String version,
@HeaderParam(CALLING_MODULE) String calling_module,
@QueryParam("requestContext") String request_context,
@QueryParam(SESSION_ID) String sessionId1,
@HeaderParam(SESSION_ID)    String sessionId2,
@Context HttpServletRequest httpRequestContext,
VirtualRoomFilter vrFilter)
{
String METHOD_NAME = "getVirtualRoomsByFilter";
log.info(METHOD_NAME + "===filter info: customer id = " + vrFilter.customerId + ", vr name = " + vrFilter.name + ", site id = " + vrFilter.siteId+ ", vr status = " + vrFilter.status + ", e164 = " + vrFilter.e164 + ", mcu id = " + vrFilter.mcuId);
BcsAuditLog esbUsageLog = handleStart(user_name, serv_name, METHOD_NAME, toPostXML(vrFilter), calling_module, getIp(httpRequestContext));

BcsResponse loginResult = loginByCredentialsOrSession(serv_name, serv_pw, user_name,  user_pw, esbUsageLog, sessionId1, sessionId2, httpRequestContext);
if(! loginResult.isSuccessful()) return  handleError("login", loginResult, esbUsageLog);

RequestContext requestContext = null;
try
{
requestContext = handleRequestContextParameter(request_context);
}
catch (Exception e)
{
return handleError("Parameter problem: ", new BcsResponseErrorAndDescr(new BcsResponseError("AA11", null), e.getMessage()), esbUsageLog);
}

try
{
log.debug("getVirtualRoomsByFilter: step 1");

BcsResponse retVal = new BcsResponse();
retVal.setSuccessful(true);
retVal.setReturnedObject(myMethods.getVirtualRoomsByFilter(vrFilter));
retVal.setWarningStr(METHOD_NAME);

return marshal(retVal, esbUsageLog, "getvrs", version, requestContext);
}
catch (java.lang.Throwable t)
{
return handleError(METHOD_NAME, t, esbUsageLog, user_name);
}
}

maven的依赖配置
<dependency>
  <groupId>junit</groupId>
<artifactId>junit</artifactId>
  <version>4.4</version>
</dependency>
  <dependency>
<groupId>org.apache.servicemix.specs</groupId>
<artifactId>org.apache.servicemix.specs.jsr311-api-0.8</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.servicemix.specs</groupId>
<artifactId>org.apache.servicemix.specs.jaxb-api-2.1</artifactId>
<version>1.3.0.0-fuse</version>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.commons-httpclient</artifactId>
<version>3.1_1-fuse</version>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.16</version>
</dependency>

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客