接口中的xml报文和实体类的之间转换

需求:湖险行业意健险保风险预警平台数据接口

 

    平台提供HTTPServlet服务,接受保险公司的数据请求,并在处理后返回给保险公司相应的处理结果。数据接口格式为XML数据报文,在每次接口的交易中,都包含了请求的XML数据与平台返回的XML数据。

          直接操作xml格式的报文是不太方便的,最好是将xml格式的报文转换成对应的实体类,再进行操作会变得清晰方便许多。

          接下来的代码是xml和实体类之间的相互转化。

  1 package com.sinosoft.serviceclient.bj;
  2 
  3 import com.sinosoft.lis.pubfun.PubFun;
  4 import com.sinosoft.serviceclient.ServiceClient;
  5 import com.sinosoft.tech.log.Logger;
  6 import com.sinosoft.tech.log.LoggerFactory;
  7 import com.sinosoft.tools.xmltools.framework.XmlConvertor;
  8 import com.sinosoft.utility.SSRS;
  9 import com.sinosoft.webservice.DTO.hnRiskWarning.contUpload.ContUpload_Body_Req;
 10 import com.sinosoft.webservice.DTO.hnRiskWarning.contUpload.ContUpload_Body_Rsp;
 11 import com.sinosoft.webservice.DTO.hnRiskWarning.contUpload.ContUpload_Constantly_Req;
 12 import com.sinosoft.webservice.DTO.hnRiskWarning.contUpload.ContUpload_Head_Req;
 13 import com.sinosoft.webservice.DTO.hnRiskWarning.contUpload.ContUpload_Head_Rsp;
 14 import com.sinosoft.webservice.DTO.hnRiskWarning.contUpload.ContUpload_Packet_Req;
 15 import com.sinosoft.webservice.DTO.hnRiskWarning.contUpload.ContUpload_Packet_Rsp;
 16 import com.sinosoft.webservice.DTO.hnRiskWarning.edorUpload.EdorUpload_Body_Req;
 17 import com.sinosoft.webservice.DTO.hnRiskWarning.edorUpload.EdorUpload_Constantly_Req;
 18 import com.sinosoft.webservice.DTO.hnRiskWarning.edorUpload.EdorUpload_Head_Req;
 19 import com.sinosoft.webservice.DTO.hnRiskWarning.edorUpload.EdorUpload_Packet_Req;
 20 import com.sinosoft.webservice.DTO.hnRiskWarning.edorUpload.EdorUpload_Packet_Rsp;
 21 import com.sinosoft.webservice.DTO.hnRiskWarning.registerUpload.RegisterUpload_Body_Req;
 22 import com.sinosoft.webservice.DTO.hnRiskWarning.registerUpload.RegisterUpload_Constantly_Req;
 23 import com.sinosoft.webservice.DTO.hnRiskWarning.registerUpload.RegisterUpload_Head_Req;
 24 import com.sinosoft.webservice.DTO.hnRiskWarning.registerUpload.RegisterUpload_Packet_Req;
 25 import com.sinosoft.webservice.DTO.hnRiskWarning.registerUpload.RegisterUpload_Packet_Rsp;
 26 import com.sinosoft.webservice.DTO.hnRiskWarning.settleUpload.SettleUpload_Body_Req;
 27 import com.sinosoft.webservice.DTO.hnRiskWarning.settleUpload.SettleUpload_Constantly_Req;
 28 import com.sinosoft.webservice.DTO.hnRiskWarning.settleUpload.SettleUpload_Head_Req;
 29 import com.sinosoft.webservice.DTO.hnRiskWarning.settleUpload.SettleUpload_Packet_Req;
 30 import com.sinosoft.webservice.DTO.hnRiskWarning.settleUpload.SettleUpload_Packet_Rsp;
 31 
 32 /**
 33  * 湖南风险预警平台-客户端
 34  * 
 35  * @author 赵永强
 36  */
 37 public class HNRiskWarningClient {
 38     static final Logger logger = LoggerFactory.getLogger(HNRiskWarningClient.class);
 39     // 系统当前日期
 40     private String mCurrentDate;
 41     // 系统当前时间
 42     private String mCurrentTime;
 43     // 转换器
 44     private XmlConvertor convertor = new XmlConvertor();
 45     // 发送请求
 46     ServiceClient serviceClient = new ServiceClient();
 47 
 48     public HNRiskWarningClient() {
 49         mCurrentTime = PubFun.getCurrentTime();
 50         mCurrentDate = PubFun.getCurrentDate();
 51     }
 52 
 53     // 需要在select * from ldinterfaceconfig l where makeoperator='' for update;
 54     // 表中进行配置
 55     // 接口的环节,分为:承保环节 理赔环节
 56 
 57     // 承保环节-保单上传
 58     public boolean contUpload(SSRS tSSRS) {
 59 
 60         // 封装报文
 61         ContUpload_Packet_Req tContUpload_Packet_Req = new ContUpload_Packet_Req();
 62         ContUpload_Head_Req tContUpload_Head_Req = new ContUpload_Head_Req();
 63         // 先给报文头附上默认值,后期需要改动再改动
 64         tContUpload_Head_Req.setRecordType("V02");
 65         tContUpload_Head_Req.setCompanyCode("C1");
 66         tContUpload_Head_Req.setUptime(mCurrentDate + " " + mCurrentTime);
 67         tContUpload_Head_Req.setSign("FC9AAD2CD4C8992D8E6D484BC0FF5C40");
 68         tContUpload_Head_Req.setUpNumber("100");
 69 
 70         ContUpload_Body_Req tContUpload_Body_Req = new ContUpload_Body_Req();
 71         // 创建对象数组
 72         ContUpload_Constantly_Req[] tUpload_Constantly_Reqs = new ContUpload_Constantly_Req[tSSRS.getMaxRow()];
 73 
 74         for (int i = 1; i <= tSSRS.getMaxRow(); i++) {
 75             ContUpload_Constantly_Req tContUpload_Constantly_Req = new ContUpload_Constantly_Req();
 76             tContUpload_Constantly_Req.setNumber(tSSRS.GetText(i, 1));
 77             tContUpload_Constantly_Req.setItemKind(tSSRS.GetText(i, 2));
 78             tContUpload_Constantly_Req.setPolicyNo(tSSRS.GetText(i, 3));
 79             tContUpload_Constantly_Req.setInsuredName(tSSRS.GetText(i, 4));
 80             tContUpload_Constantly_Req.setInsuredIdType(tSSRS.GetText(i, 5));
 81             tContUpload_Constantly_Req.setInsuredIdCard(tSSRS.GetText(i, 6));
 82             tContUpload_Constantly_Req.setSellChannel(tSSRS.GetText(i, 7));
 83             tContUpload_Constantly_Req.setStartDate(tSSRS.GetText(i, 8));
 84             tContUpload_Constantly_Req.setEndDate(tSSRS.GetText(i, 9));
 85             tContUpload_Constantly_Req.setEnterTime(tSSRS.GetText(i, 10));
 86             tContUpload_Constantly_Req.setAmount(tSSRS.GetText(i, 11));
 87             tContUpload_Constantly_Req.setAreaId(tSSRS.GetText(i, 12));
 88 
 89             tUpload_Constantly_Reqs[i - 1] = tContUpload_Constantly_Req;
 90 
 91         }
 92 
 93         tContUpload_Body_Req.setContUpload_Constantly(tUpload_Constantly_Reqs);
 94         tContUpload_Packet_Req.setBody(tContUpload_Body_Req);
 95         tContUpload_Packet_Req.setHead(tContUpload_Head_Req);
 96         // 调用webService接口发送数据,并获取返回的xml数据
 97         byte[] requestXML = convertor.convertToXml(tContUpload_Packet_Req, "");
 98         String mStr = new String(requestXML);
 99         logger.info(" 承保环节-保单上传-请求报文:" + mStr);
100         String returnXML = "";// 接收预警平台传过来的返回报文
101         try {
102             // TODO callService方法中参数,需后续配置
103             returnXML = serviceClient.callService("", "", mStr);
104             // 对湖南预警平台返回的报文进行解析并正确和错误结果
105             ContUpload_Packet_Rsp tContUpload_Packet_Rsp = convertor.convertToObject(returnXML, ContUpload_Packet_Rsp.class);
106             if ("0".equals(tContUpload_Packet_Rsp.getHead().getErrorNumber())) {
107                 logger.info("推送数据-[承保环节-保单上传]-完成,调用接口成功");
108                 return true;
109             }
110             if ("2".equals(tContUpload_Packet_Rsp.getHead().getErrorNumber())) {
111                 logger.info("推送数据-[承保环节-保单上传]-失败,调用接口失败");
112                 return false;
113             }
114 
115         } catch (Exception e1) {
116             logger.error("调用湖南保险行业意健险承保风险预警平台数据接口-[承保环节-保单上传]-报错了!", e1);
117             logger.info("调用湖南保险行业意健险承保风险预警平台数据接口-[承保环节-保单上传]-结束!");
118             e1.printStackTrace();
119             return false;
120         }
121         return true;
122 
123     }
124 
125     // 承保环节-批单上传
126     public boolean edorUpload(SSRS tSSRS) {
127 
128         // 封装报文
129         EdorUpload_Packet_Req tEdorUpload_Packet_Req = new EdorUpload_Packet_Req();
130         EdorUpload_Head_Req tEdorUpload_Head_Req = new EdorUpload_Head_Req();
131         // 先给报文头附上默认值,后期需要改动再改动
132         tEdorUpload_Head_Req.setRecordType("V03");
133         tEdorUpload_Head_Req.setCompanyCode("C1");
134         tEdorUpload_Head_Req.setUptime(mCurrentDate + " " + mCurrentTime);
135         tEdorUpload_Head_Req.setSign("FC9AAD2CD4C8992D8E6D484BC0FF5C40");
136         tEdorUpload_Head_Req.setUpNumber("100");
137 
138         EdorUpload_Body_Req tEdorUpload_Body_Req = new EdorUpload_Body_Req();
139         // 创建对象数组
140         EdorUpload_Constantly_Req[] tEdorUpload_Constantly_Reqs = new EdorUpload_Constantly_Req[tSSRS.getMaxRow()];
141 
142         for (int i = 1; i <= tSSRS.getMaxRow(); i++) {
143             EdorUpload_Constantly_Req tEdorUpload_Constantly_Req = new EdorUpload_Constantly_Req();
144             tEdorUpload_Constantly_Req.setNumber(tSSRS.GetText(i, 1));
145             tEdorUpload_Constantly_Req.setItemKind(tSSRS.GetText(i, 2));
146             tEdorUpload_Constantly_Req.setEndorseNo(tSSRS.GetText(i, 3));
147             tEdorUpload_Constantly_Req.setPolicyNo(tSSRS.GetText(i, 4));
148             tEdorUpload_Constantly_Req.setIniInsuredName(tSSRS.GetText(i, 5));
149             tEdorUpload_Constantly_Req.setIniInsuredIdType(tSSRS.GetText(i, 6));
150             tEdorUpload_Constantly_Req.setIniInsuredIdCard(tSSRS.GetText(i, 7));
151             tEdorUpload_Constantly_Req.setInsuredName(tSSRS.GetText(i, 8));
152             tEdorUpload_Constantly_Req.setInsuredIdType(tSSRS.GetText(i, 9));
153             tEdorUpload_Constantly_Req.setInsuredIdCard(tSSRS.GetText(i, 10));
154             tEdorUpload_Constantly_Req.setSellChannel(tSSRS.GetText(i, 11));
155             tEdorUpload_Constantly_Req.setStartDate(tSSRS.GetText(i, 12));
156             tEdorUpload_Constantly_Req.setEndDate(tSSRS.GetText(i, 13));
157             tEdorUpload_Constantly_Req.setEnterTime(tSSRS.GetText(i, 14));
158             tEdorUpload_Constantly_Req.setAmount(tSSRS.GetText(i, 15));
159             tEdorUpload_Constantly_Req.setAreaId(tSSRS.GetText(i, 16));
160 
161             tEdorUpload_Constantly_Reqs[i - 1] = tEdorUpload_Constantly_Req;
162 
163         }
164 
165         tEdorUpload_Body_Req.setEdorUpload_Constantly_Req(tEdorUpload_Constantly_Reqs);
166         tEdorUpload_Packet_Req.setBody(tEdorUpload_Body_Req);
167         tEdorUpload_Packet_Req.setHead(tEdorUpload_Head_Req);
168         // 调用webService接口发送数据,并获取返回的xml数据
169         byte[] requestXML = convertor.convertToXml(tEdorUpload_Packet_Req, "");
170         String mStr = new String(requestXML);
171         logger.info(" 承保环节-批单-请求报文:" + mStr);
172         String returnXML = "";// 接收预警平台传过来的返回报文
173         try {
174             // TODO callService方法中参数,需后续配置
175             returnXML = serviceClient.callService("", "", mStr);
176             // 对湖南预警平台返回的报文进行解析并正确和错误结果
177             EdorUpload_Packet_Rsp tEdorUpload_Packet_Rsp = convertor.convertToObject(returnXML, EdorUpload_Packet_Rsp.class);
178             if ("0".equals(tEdorUpload_Packet_Rsp.getHead().getErrorNumber())) {
179                 logger.info("推送数据-[承保环节-批单上传]-完成,调用接口成功");
180                 return true;
181             }
182             if ("2".equals(tEdorUpload_Packet_Rsp.getHead().getErrorNumber())) {
183                 logger.info("推送数据-[承保环节-批单上传]-失败,调用接口失败");
184                 return false;
185             }
186 
187         } catch (Exception e1) {
188             logger.error("调用湖南保险行业意健险承保风险预警平台数据接口-[承保环节-批单上传]-报错了!", e1);
189             logger.info("调用湖南保险行业意健险承保风险预警平台数据接口-[承保环节-批单上传]-结束!");
190             e1.printStackTrace();
191             return false;
192         }
193         return true;
194 
195     }
196 
197     // 理赔部分-立案上传
198     public boolean registerUpload(SSRS tSSRS) {
199 
200         // 封装报文
201         RegisterUpload_Packet_Req tRegisterUpload_Packet_Req = new RegisterUpload_Packet_Req();
202         RegisterUpload_Head_Req tRegisterUpload_Head_Req = new RegisterUpload_Head_Req();
203         // 先给报文头附上默认值,后期需要改动再改动
204         tRegisterUpload_Head_Req.setRecordType("V04");
205         tRegisterUpload_Head_Req.setCompanyCode("C1");
206         tRegisterUpload_Head_Req.setUptime(mCurrentDate + " " + mCurrentTime);
207         tRegisterUpload_Head_Req.setSign("FC9AAD2CD4C8992D8E6D484BC0FF5C40");
208         tRegisterUpload_Head_Req.setUpNumber("100");
209 
210         RegisterUpload_Body_Req tRegisterUpload_Body_Req = new RegisterUpload_Body_Req();
211         // 创建对象数组
212         RegisterUpload_Constantly_Req[] tRegisterUpload_Constantly_Reqs = new RegisterUpload_Constantly_Req[tSSRS.getMaxRow()];
213 
214         for (int i = 1; i <= tSSRS.getMaxRow(); i++) {
215             RegisterUpload_Constantly_Req tRegisterUpload_Constantly_Req = new RegisterUpload_Constantly_Req();
216             tRegisterUpload_Constantly_Req.setNumber(tSSRS.GetText(i, 1));
217             tRegisterUpload_Constantly_Req.setItemKind(tSSRS.GetText(i, 2));
218             tRegisterUpload_Constantly_Req.setReportNo(tSSRS.GetText(i, 3));
219             tRegisterUpload_Constantly_Req.setPolicyNo(tSSRS.GetText(i, 4));
220             tRegisterUpload_Constantly_Req.setInsuredName(tSSRS.GetText(i, 5));
221             tRegisterUpload_Constantly_Req.setInsuredIdType(tSSRS.GetText(i, 6));
222             tRegisterUpload_Constantly_Req.setInsuredIdCard(tSSRS.GetText(i, 7));
223             tRegisterUpload_Constantly_Req.setReportDate(tSSRS.GetText(i, 8));
224             tRegisterUpload_Constantly_Req.setLossDate(tSSRS.GetText(i, 9));
225             tRegisterUpload_Constantly_Req.setClaimDate(tSSRS.GetText(i, 10));
226             tRegisterUpload_Constantly_Req.setLossAmount(tSSRS.GetText(i, 11));
227             tRegisterUpload_Constantly_Req.setLossAddress(tSSRS.GetText(i, 12));
228             tRegisterUpload_Constantly_Reqs[i - 1] = tRegisterUpload_Constantly_Req;
229 
230         }
231 
232         tRegisterUpload_Body_Req.setRegisterUpload_Constantly_Req(tRegisterUpload_Constantly_Reqs);
233         tRegisterUpload_Packet_Req.setBody(tRegisterUpload_Body_Req);
234         tRegisterUpload_Packet_Req.setHead(tRegisterUpload_Head_Req);
235         // 调用webService接口发送数据,并获取返回的xml数据
236         byte[] requestXML = convertor.convertToXml(tRegisterUpload_Packet_Req, "");
237         String mStr = new String(requestXML);
238         logger.info(" 理赔环节-立案上传-请求报文:" + mStr);
239         String returnXML = "";// 接收预警平台传过来的返回报文
240         try {
241             // TODO callService方法中参数,需后续配置
242             returnXML = serviceClient.callService("", "", mStr);
243             // 对湖南预警平台返回的报文进行解析并正确和错误结果
244             RegisterUpload_Packet_Rsp tRegisterUpload_Packet_Rsp = convertor.convertToObject(returnXML, RegisterUpload_Packet_Rsp.class);
245             if ("0".equals(tRegisterUpload_Packet_Rsp.getHead().getErrorNumber())) {
246                 logger.info("推送数据-[理赔环节-立案上传]-完成,调用接口成功");
247                 return true;
248             }
249             if ("2".equals(tRegisterUpload_Packet_Rsp.getHead().getErrorNumber())) {
250                 logger.info("推送数据-[理赔环节-立案上传]-失败,调用接口失败");
251                 return false;
252             }
253         } catch (Exception e1) {
254             logger.error("调用湖南保险行业意健险承保风险预警平台数据接口-[理赔环节-立案上传]-报错了!", e1);
255             logger.info("调用湖南保险行业意健险承保风险预警平台数据接口-[理赔环节-立案上传]-结束!");
256             e1.printStackTrace();
257             return false;
258         }
259         return true;
260     }
261 
262     // 理赔部分-结案上传
263     public boolean settleUpload(SSRS tSSRS) {
264         // 封装报文
265         SettleUpload_Packet_Req tSettleUpload_Packet_Req = new SettleUpload_Packet_Req();
266         SettleUpload_Head_Req tSettleUpload_Head_Req = new SettleUpload_Head_Req();
267         // 先给报文头附上默认值,后期需要改动再改动
268         tSettleUpload_Head_Req.setRecordType("V05");
269         tSettleUpload_Head_Req.setCompanyCode("C1");
270         // tSettleUpload_Head_Req.setUptime("2016-12-19 10:47:50");
271         tSettleUpload_Head_Req.setUptime(mCurrentDate + " " + mCurrentTime);
272         tSettleUpload_Head_Req.setSign("FC9AAD2CD4C8992D8E6D484BC0FF5C40");
273         tSettleUpload_Head_Req.setUpNumber("100");
274 
275         SettleUpload_Body_Req tSettleUpload_Body_Req = new SettleUpload_Body_Req();
276         // 创建对象数组
277         SettleUpload_Constantly_Req[] tSettleUpload_Constantly_Reqs = new SettleUpload_Constantly_Req[tSSRS.getMaxRow()];
278 
279         for (int i = 1; i <= tSSRS.getMaxRow(); i++) {
280             SettleUpload_Constantly_Req tSettleUpload_Constantly_Req = new SettleUpload_Constantly_Req();
281             tSettleUpload_Constantly_Req.setNumber(tSSRS.GetText(i, 1));
282             tSettleUpload_Constantly_Req.setItemKind(tSSRS.GetText(i, 2));
283             tSettleUpload_Constantly_Req.setReportNo(tSSRS.GetText(i, 3));
284             tSettleUpload_Constantly_Req.setPolicyNo(tSSRS.GetText(i, 4));
285             tSettleUpload_Constantly_Req.setInsuredName(tSSRS.GetText(i, 5));
286             tSettleUpload_Constantly_Req.setInsuredIdType(tSSRS.GetText(i, 6));
287             tSettleUpload_Constantly_Req.setInsuredIdCard(tSSRS.GetText(i, 7));
288             tSettleUpload_Constantly_Req.setEndCaseDate(tSSRS.GetText(i, 8));
289             tSettleUpload_Constantly_Req.setPayAmount(tSSRS.GetText(i, 9));
290 
291             tSettleUpload_Constantly_Reqs[i - 1] = tSettleUpload_Constantly_Req;
292 
293         }
294 
295         tSettleUpload_Body_Req.setSettleUpload_Constantly_Req(tSettleUpload_Constantly_Reqs);
296         tSettleUpload_Packet_Req.setBody(tSettleUpload_Body_Req);
297         tSettleUpload_Packet_Req.setHead(tSettleUpload_Head_Req);
298         // 调用webService接口发送数据,并获取返回的xml数据
299         byte[] requestXML = convertor.convertToXml(tSettleUpload_Packet_Req, "");
300         String mStr = new String(requestXML);
301         logger.info(" 理赔环节-结案上传-请求报文:" + mStr);
302         String returnXML = "";// 接收预警平台传过来的返回报文
303         try {
304             // TODO callService方法中参数,需后续配置
305             returnXML = serviceClient.callService("", "", mStr);
306             SettleUpload_Packet_Rsp tSettleUpload_Packet_Rsp = convertor.convertToObject(returnXML, SettleUpload_Packet_Rsp.class);
307             if ("0".equals(tSettleUpload_Packet_Rsp.getHead().getErrorNumber())) {
308                 logger.info("推送数据-[理赔环节-结案上传]-完成,调用接口成功");
309                 return true;
310             }
311             if ("2".equals(tSettleUpload_Packet_Rsp.getHead().getErrorNumber())) {
312                 logger.info("推送数据-[理赔环节-结案上传]-失败,调用接口失败");
313                 return false;
314             }
315         } catch (Exception e1) {
316             logger.error("调用湖南保险行业意健险承保风险预警平台数据接口-[理赔环节-结案上传]-报错了!", e1);
317             logger.info("调用湖南保险行业意健险承保风险预警平台数据接口-[理赔环节-结案上传]-结束!");
318             e1.printStackTrace();
319             return false;
320         }
321         return true;
322     }
323 
324     /**
325      * 测试方法
326      * 
327      * @param args
328      */
329     public static void main(String[] args) {
330         // HNRiskWarningClient hnRiskWarningClient=new HNRiskWarningClient();
331         // InsureQuery_Packet_Req req=new InsureQuery_Packet_Req();
332         // hnRiskWarningClient.queryInsurance(req);
333         XmlConvertor convertor = new XmlConvertor();
334         ContUpload_Packet_Rsp tContUpload_Packet_Rsp = new ContUpload_Packet_Rsp();
335         ContUpload_Body_Rsp tUpload_Body_Rsp = new ContUpload_Body_Rsp();
336         ContUpload_Head_Rsp tUpload_Head_Rsp = new ContUpload_Head_Rsp();
337         tUpload_Head_Rsp.setErrorNumber("2");
338         tUpload_Head_Rsp.setUpNumber("100");
339         tUpload_Head_Rsp.setRecordType("V02");
340         tContUpload_Packet_Rsp.setBody(tUpload_Body_Rsp);
341         tContUpload_Packet_Rsp.setHead(tUpload_Head_Rsp);
342         byte[] convertToXml = convertor.convertToXml(tContUpload_Packet_Rsp, "gbk");
343         String str = new String(convertToXml);
344         System.out.println("请求参数:" + str);
345         tContUpload_Packet_Rsp = convertor.convertToObject(str, ContUpload_Packet_Rsp.class);
346         if ("0".equals(tContUpload_Packet_Rsp.getHead().getErrorNumber())) {
347             logger.info("推送数据-[承保环节-保单上传]-完成,调用接口成功");
348 
349         }
350         if ("2".equals(tContUpload_Packet_Rsp.getHead().getErrorNumber())) {
351             logger.info("推送数据-[承保环节-保单上传]-失败,调用接口失败");
352         }
353         String currentTime = PubFun.getCurrentTime();
354         System.out.println("currentTime:" + currentTime);
355     }
356 
357 }

 

posted on 2019-09-04 14:33  ~码铃薯~  阅读(2381)  评论(0编辑  收藏  举报

导航