新需求:提供给第三方一个接口,接受他们的请求报文,我们系统返回相应的返回报文。
方法1:WebService方法
定义接口、实现类,发布出去即可,提供给第三方一个wsdl地址即可。
方法二:由于两个系统不是直接交互,我们这儿的服务要通过ESB中转,我们的是SOAP报文,第三方的是SOP报文;
第三方先发SOP到ESB,ESB请求我们,然后将相应的结果返回给第三方。
思路:我们不管第三方,相当于直接跟ESB互相沟通,所以我实现一个Servlet,接受ESB的请求,
1.解析请求报文,获取我要的参数
2,拿到参数,完成业务逻辑处理;
3.组装返回报文,回传给ESB。
4.ESB传给第三方调用方。
方法1:WebService方法
定义接口、实现类,发布出去即可,提供给第三方一个wsdl地址即可。
方法二:由于两个系统不是直接交互,我们这儿的服务要通过ESB中转,我们的是SOAP报文,第三方的是SOP报文;
第三方先发SOP到ESB,ESB请求我们,然后将相应的结果返回给第三方。
思路:我们不管第三方,相当于直接跟ESB互相沟通,所以我实现一个Servlet,接受ESB的请求,
1.解析请求报文,获取我要的参数
2,拿到参数,完成业务逻辑处理;
3.组装返回报文,回传给ESB。
4.ESB传给第三方调用方。
具体代码如下:
package ***;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.topcheer.action.common.ParseXml;
import org.topcheer.fal.util.FalUtils;
/**
* Servlet implementation class QryService
* Author:shigx
* Date:2017-09-13
*/
public class QryService extends HttpServlet {
private static final long serialVersionUID = 1L;
private final Logger logger=Logger.getLogger(getClass());
JdbcTemplate jdbcTemplate = FalUtils.getJDBCTemplate(FalUtils.getDataSource());
String url ="http://localhost:7001/App/UnfSocCrdtNoQry";
String targetSysId = "0150";
/**
* @see HttpServlet#HttpServlet()
*/
public QryService() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
/*InputStream is = request.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String soap =null;
try{
while((soap = bf.readLine())!=null){
sb.append(soap);
}
}catch(Exception e){
e.printStackTrace();
}
String s = sb.toString();*/
String customerCode ="";
//String msgId = "111111111111111";
String backEndSeqNo = "";
String mac="";
Map map =new HashMap();
String method = request.getMethod();
if(method.equalsIgnoreCase("GET")){
InputStream is = request.getInputStream();//获取输入流信息
BufferedReader bf = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String soap =null;
try{
while((soap = bf.readLine())!=null){
sb.append(soap);
}
String xml = sb.toString();
logger.info("请求报文:"+xml);
map = ParseXml.getCustomerCode(xml);
if(map.get("OrgInstId")!=null)
customerCode = map.get("OrgInstId").toString();
if(map.get("TranSeqNo")!=null)
backEndSeqNo = map.get("TranSeqNo").toString();
if(map.get("Mac")!=null)
mac = map.get("Mac").toString();
}catch(Exception e){
logger.error(e);
e.printStackTrace();
}
}else if(method.equalsIgnoreCase("POST")){
InputStream is = request.getInputStream();//获取输入流信息
BufferedReader bf = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String soap =null;
try{
while((soap = bf.readLine())!=null){
sb.append(soap);
}
String xml = sb.toString();
logger.info("请求报文:"+xml);
map = ParseXml.getCustomerCode(xml);
if(map.get("OrgInstId")!=null)
customerCode = map.get("OrgInstId").toString();
if(map.get("TranSeqNo")!=null)
backEndSeqNo = map.get("TranSeqNo").toString();
if(map.get("Mac")!=null)
mac = map.get("Mac").toString();
}catch(Exception e){
logger.error(e);
e.printStackTrace();
}
}
List parameter = new ArrayList();
String sql = "SELECT * FROM TBL_CUSTOMER WHERE CUSTOMERCODE = '"+customerCode+"' AND STATE ='02'";
//logger.info("查询SQL:"+sql);
List<Map<String,Object>> list =jdbcTemplate.queryForList(sql);
int len =list.size();
//logger.info("长度:"+len);
StringBuffer str = new StringBuffer();
if(len==0){//无结果返回的报文
String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
String time = new SimpleDateFormat("HHmmss").format(new Date());
str.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" ")
.append("xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" ")
.append("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ")
.append("xmlns:s=\"").append(url).append("\" ")
.append("xmlns:d=\"http://esb.spdbbiz.com/metadata\">")
.append("<soap:Header>")
.append("<s:RspHeader>")
.append("<d:Mac>").append(mac).append("</d:Mac>")
//.append("<d:MacOrgId>888888</d:MacOrgId>")
.append("<d:TargetSysId>").append(targetSysId).append("</d:TargetSysId>")
//.append("<d:MsgId>").append(msgId).append("</d:MsgId>")
.append("</s:RspHeader>")
.append("</soap:Header>")
.append("<soap:Body>")
.append("<s:RspUnfSocCrdtNoQry>")
.append("<s:RspSvcHeader>")
.append("<s:TranDate>").append(date).append("</s:TranDate>")
.append("<s:TranTime>").append(time).append("</s:TranTime>")
.append("<s:BackendSysId>0150</s:BackendSysId>")
.append("<s:BackendSeqNo>").append(backEndSeqNo).append("</s:BackendSeqNo>")
.append("<s:ReturnCode>000000000001</s:ReturnCode>")
.append("<s:ReturnMsg>交易失败:无结果返回</s:ReturnMsg>")
.append("</s:RspSvcHeader>")
.append("<s:SvcBody>")
.append("<s:OrgInstId></s:OrgInstId>")
.append("<s:OrgInstNm></s:OrgInstNm>")
.append("<s:UnfSocCrdtNo></s:UnfSocCrdtNo>")
.append("<s:RecordStatus2></s:RecordStatus2>")
.append("<s:EffectDate></s:EffectDate>")
.append("</s:SvcBody>")
.append("</s:RspUnfSocCrdtNoQry>")
.append("</soap:Body>")
.append("</soap:Envelope>");
}else{//正常返回的报文
String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
String time = new SimpleDateFormat("HHmmss").format(new Date());
str.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"")
.append("xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" ")
.append("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"")
.append("xmlns:s=\"").append(url).append("\"")
.append("xmlns:d=\"http://esb.spdbbiz.com/metadata\">")
.append("<soap:Header>")
.append("<s:RspHeader>")
.append("<d:Mac>").append(mac).append("</d:Mac>")
//.append("<d:MacOrgId>888888</d:MacOrgId>")
.append("<d:TargetSysId>").append(targetSysId).append("</d:TargetSysId>")
//.append("<d:MsgId>").append(msgId).append("</d:MsgId>")
.append("</s:RspHeader>")
.append("</soap:Header>")
.append("<soap:Body>")
.append("<s:RspUnfSocCrdtNoQry>")
.append("<s:RspSvcHeader>")
.append("<s:TranDate>").append(date).append("</s:TranDate>")
.append("<s:TranTime>").append(time).append("</s:TranTime>")
.append("<s:BackendSysId>0150</s:BackendSysId>")
.append("<s:BackendSeqNo>").append(backEndSeqNo).append("</s:BackendSeqNo>")
.append("<s:ReturnCode>000000000000</s:ReturnCode>")
.append("<s:ReturnMsg>交易成功</s:ReturnMsg>")
.append("</s:RspSvcHeader>")
.append("<s:SvcBody>");
for(int i=0;i<len;i++){
if(list.get(i).get("CUSTOMERCODE")!=null){
String code = list.get(i).get("CUSTOMERCODE").toString();
if(code!=null)
str.append("<s:OrgInstId>").append(code).append("</s:OrgInstId>");
}
else
str.append("<s:OrgInstId>").append("</s:OrgInstId>");
if(list.get(i).get("CUSTOMERNAME")!=null){
String name = list.get(i).get("CUSTOMERNAME").toString();
if(name!=null)
str.append("<s:OrgInstNm>").append(name).append("</s:OrgInstNm>");
}
else
str.append("<s:OrgInstNm>").append("</s:OrgInstNm>");
if(list.get(i).get("CREDITCODE")!=null){
String crdcode = list.get(i).get("CREDITCODE").toString();
if(crdcode!=null)
str.append("<s:UnfSocCrdtNo>").append(crdcode).append("</s:UnfSocCrdtNo>");
}
else
str.append("<s:UnfSocCrdtNo>").append("</s:UnfSocCrdtNo>");
if(list.get(i).get("STATE")!=null){
String state = list.get(i).get("STATE").toString();
if(state!=null)
str.append("<s:RecordStatus2>").append(state).append("</s:RecordStatus2>");
}
else
str.append("<s:RecordStatus2>").append("</s:RecordStatus2>");
if(list.get(i).get("EFFECTDATE")!=null){
String mdate = list.get(i).get("EFFECTDATE").toString();
if(date!=null)
str.append("<s:EffectDate>").append(mdate).append("</s:EffectDate>");
}
else
str.append("<s:EffectDate>").append("</s:EffectDate>");
}
str.append("</s:SvcBody>")
.append("</s:RspUnfSocCrdtNoQry>")
.append("</soap:Body>")
.append("</soap:Envelope>");
}
/*System.out.println(str.toString());
PrintWriter out =response.getWriter();
out.write(str.toString());*/
//OutputStream out =response.getOutputStream();
response.setHeader("content-type","text/xml;charset=UTF-8");
logger.info("返回报文:"+str.toString());
byte[] by = (str.toString()).getBytes("utf-8");
PrintWriter out =response.getWriter();
out.write(str.toString());
//out.write(by);
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request,response);
}
}
这是XML解析的工具类以及测试类。
package ***;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class ParseXml {
private static String str = "";
static String url ="http://localhost:7001/App/UnfSocCrdtNoQry";
static String targetSysId = "00000000000";
static String msgId = "11111111";
String returnCode = "000000000000";
public static Map<String,Object> map =new HashMap<String,Object>();
public static Map getCustomerCode(String Soap) throws DocumentException {
Document doc = DocumentHelper.parseText(Soap);
Element root = doc.getRootElement();
getCode(root);
return map;
}
public static void getCode(Element root) throws DocumentException {
if (root.elements() != null) {
List<Element> list = root.elements();
for (Element e : list) {
if (e.elements().size() > 0) {
//map.put(e.getName(),e.getTextTrim());
getCode(e);
}
if (e.elements().size()==0) {
map.put(e.getName(),e.getTextTrim());
}
}
}
}
public static String connect(String urlString,StringBuilder stringBuilder ){
System.out.println(stringBuilder.toString());
URL url= null;
HttpURLConnection httpURLConnection=null;
OutputStream outputStream=null;
InputStream inputStream=null;
HashMap<String,String>map=null;
try {
url = new URL(urlString);
httpURLConnection= (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setUseCaches(false);
httpURLConnection.setInstanceFollowRedirects(true);
httpURLConnection.setRequestProperty("Content-Type","text/html; charset=UTF-8");
httpURLConnection.connect();
outputStream= httpURLConnection.getOutputStream();
outputStream.write(stringBuilder.toString().getBytes());
outputStream.flush();
outputStream.close();
if(httpURLConnection.getResponseCode()==200){
byte [] buf=new byte[4096];
inputStream=httpURLConnection.getInputStream();
StringBuilder xmlStringbuilder=new StringBuilder("");
int len=0;//inputStream.read(buf);
while((len=inputStream.read(buf))!=-1){
xmlStringbuilder.append(new String(buf,0,len,"UTF-8"));
}
//xmlString=new String(buf,0,len,"UTF-8");
System.out.println("---------------------------------------------------------");
System.out.println(xmlStringbuilder.toString());
return xmlStringbuilder.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static void main(String [] args) throws MalformedURLException{
StringBuilder st = new StringBuilder();
/*String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
String time = new SimpleDateFormat("HHmmss").format(new Date());
st.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:s=\"http://localhost:7001/App/QryService/\" xmlns:d=\"http://esb.spdbbiz.com/metadata\">")
.append("<soap:Header>")
.append("<s:ReqHeader>")
.append("<d:Mac>").append("00000000000000000000").append("</d:Mac>")
.append("<d:MacOrgId>888888</d:MacOrgId>")
.append("<d:TargetSysId>").append(targetSysId).append("</d:TargetSysId>")
.append("<d:MsgId>").append(msgId).append("</d:MsgId>")
.append("</s:ReqHeader>")
.append("</soap:Header>")
.append("<soap:Body>")
.append("<s:UnfSocCrdtNoQry>")
.append("<s:ReqSvcHeader>")
.append("<s:TranDate>").append(date).append("</s:TranDate>")
.append("<s:TranTime>").append(time).append("</s:TranTime>")
.append("<s:BackendSysId>0011</s:BackendSysId>")
.append("<s:BackendSeqNo>0011</s:BackendSeqNo>")
.append("<s:ReturnCode>000000000000</s:ReturnCode>")
.append("<s:ReturnMsg>Success</s:ReturnMsg>")
.append("</s:ReqSvcHeader>")
.append("<s:SvcBody>")
.append("<s:OrgInstId>").append("000000668").append("</s:OrgInstId>")
.append("</s:SvcBody>")
.append("</s:UnfSocCrdtNoQry>")
.append("</soap:Body>")
.append("</soap:Envelope>");*/
st.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><soap:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:s=\"http://esb.spdbbiz.com/services/S120030432\" xmlns:d=\"http://esb.spdbbiz.com/metadata\" ><soap:Header><s:ReqHeader><d:Mac>0000000000000000</d:Mac><d:MacOrgId></d:MacOrgId><d:SourceSysId>0825</d:SourceSysId><d:ConsumerId>0825</d:ConsumerId><d:ServiceAdr>http://esb.spdbbiz.com:7701/services/S120030432</d:ServiceAdr><d:ServiceAction>urn:/UnfSocCrdtNoQry</d:ServiceAction><d:ExtendContent>00</d:ExtendContent></s:ReqHeader></soap:Header><soap:Body><s:ReqUnfSocCrdtNoQry><s:ReqSvcHeader><s:TranDate>20170913</s:TranDate><s:TranTime>162116290</s:TranTime><s:TranTellerNo>00000000</s:TranTellerNo><s:TranSeqNo></s:TranSeqNo><s:ConsumerId>0825</s:ConsumerId><s:GlobalSeqNo>15800001016431580643158064</s:GlobalSeqNo><s:SourceSysId>0825</s:SourceSysId><s:BranchId></s:BranchId><s:TerminalCode>1a</s:TerminalCode><s:CityCode>021</s:CityCode><s:AuthrTellerNo></s:AuthrTellerNo><s:AuthrPwd></s:AuthrPwd><s:AuthrCardFlag></s:AuthrCardFlag><s:AuthrCardNo></s:AuthrCardNo><s:TranCode>DM91</s:TranCode><s:PIN>1234567890123</s:PIN><s:SysOffset1>0000</s:SysOffset1><s:SysOffset2>0000</s:SysOffset2><s:MsgEndFlag>1</s:MsgEndFlag><s:MsgSeqNo>3000</s:MsgSeqNo><s:SubTranCode></s:SubTranCode><s:TranMode></s:TranMode><s:TranSerialNo>0</s:TranSerialNo></s:ReqSvcHeader><s:SvcBody><s:OrgInstId>100000024</s:OrgInstId></s:SvcBody></s:ReqUnfSocCrdtNoQry></soap:Body></soap:Envelope> ");
String so = connect(url,st);
System.out.println(so);
try {
str = st.toString();
map = getCustomerCode(str);
//if(map.get("ss")!=null)
System.out.println(map.get("ss").toString());
System.out.println(map.get("OrgInstId").toString());
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}