使用IDEA的webservice工具解析生成的客户端调用远程接口

由于这个接口的报文格式比较麻烦,是XML的请求头加上JSON格式的请求体,所以看起来比较复杂,也可以用RPC的方式调用,那样需要将请求头和请求体,响应头和响应体建实体。

public JSONObject sendReq(PortTypeEnum portTypeEnum,JSONObject reqBody){
Main main = new Main();
MainPortType mainPortType = main.getMainHttpSoap11Endpoint();
SendXmlResponse sendXmlResponse = new SendXmlResponse();
sendXmlResponse.setOutBody("");
sendXmlResponse.setOutHead("");

//修改请求URL
BindingProvider bp = (BindingProvider)mainPortType;
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,getEndPointURL());
//设置请求头信息
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//拼接请求头信息
String sendHead = "<![CDATA[<?xml version=\"1.0 \"encoding\" UTF-8\" ?>" +
"<reqInfo>" +
"<clientID>" + getClientID() + "</clientID>" +
"<reqTime>" + sf.format(new Date()) + "</reqTime>" +
"<tarService>" + portTypeEnum.getPortValue() + "</tarService>" +
"<reqPWD>" + MD5Util.MD5(getReqPWD()) + "</reqPWD>" +
"<Code></Code>" +
"<Message></Message>" +
"</reqInfo>]]>";
//响应头体信息
Holder<String> outHead = new Holder<String>(sendXmlResponse.getOutHead());
Holder<String> outBody = new Holder<String>(sendXmlResponse.getOutBody());
logger.info("调用远程共享接口,请求方法:{}", portTypeEnum.getDescribe());
logger.info("调用远程共享接口,请求参数:{}", reqBody.toJSONString());
//发送请求
mainPortType.sendXml(sendHead, reqBody.toJSONString(), outHead, outBody);
logger.info("调用远程共享接口,返回头:{}", outHead.value);
logger.info("调用远程共享接口,返回体:{}", outBody.value);

//记录日志
FaiBusinessLog log = new FaiBusinessLog();
log.setOperCreateTime(sf.format(new Date()));
log.setOperMethod(portTypeEnum.getPortName());
log.setOperUserId(UserContextHolder.getCurrentUser().getUserCode());
log.setOperUserName(UserContextHolder.getCurrentUser().getUserNameCn());
log.setOperRequest(sendHead + "\n" + reqBody.toJSONString());
log.setOperResponse(outHead.value + "\n" + outBody.value);
logger.info(log.toString());
faiBusinessLogService.save(log);

JSONObject re = new JSONObject();
if (outBody.value == null){
JSONObject jsonObject = OutHead.xmlToJSONObject(outHead.value);
logger.info("远程接口调用失败,{}",jsonObject);
return jsonObject;
}
logger.info("调用远程共享接口,返回参数:{}",outBody.value);
re.getJSONObject(outBody.value);
re = JSONObject.parseObject(outBody.value);
if (re.getString("status") == null){
re.put("status","共享接口返回为null,请联系共享人员处理");
}
re.put("sendBody",reqBody.toJSONString());
return re;
}
上一篇:经久不衰的远古时代产物——WebService。


下一篇:你对webservice的体会