`

SOAP方式发送XML调用Web Services

阅读更多

    客户有一个需求,发布了一个Web Services,其中一个Function,输入的请求参数是Soap协议的XML文件,通过请求后.会输出一个XML格式的文件.

    涉及的技术有Freemaker.

    基本的思路是:通过一个连接请求,发送soap方式的XML请求文档,然后通过读取流的方式,返回结果.

 

private String serviceAddress = "http://localhost:8080/axis/services/Corticon";
private URL webServiceUrl;

@PostConstruct
private void init() {
	try {
		webServiceUrl = new URL(serviceAddress);
	} catch (MalformedURLException e) {
		e.printStackTrace();
	}
}
public String processRequest(DialogDecisionInput input) {
		
		try {
			cfg.setClassForTemplateLoading(this.getClass(), "/");
			cfg.setObjectWrapper(new DefaultObjectWrapper());
			cfg.setNumberFormat("################");

			// Get and config the connection.
			HttpURLConnection conn;
			conn = (HttpURLConnection) webServiceUrl.openConnection();

			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setRequestMethod("POST");
			conn.setUseCaches(false);
			conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
			conn.setRequestProperty("SOAPAction", "Corticon");
			

			Map root = new HashMap();
			root.put("model", input);			

			Template template = cfg.getTemplate("soapRequest.ftl");

			OutputStream os = conn.getOutputStream();
			Writer out = new BufferedWriter(new OutputStreamWriter(os, "utf-8"));

			template.process(root, out);

			out.flush();
			out.close();
			

			// Get the soap response.
			InputStream is = conn.getInputStream();
			String rc = IOUtils.toString(is);			
			return rc;
		} catch (TemplateException e) {
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw));
			corticonlogger.error("[E00004]" + E00004 + "\n" + sw);
		} catch (IOException e1) {
			StringWriter sw = new StringWriter();
			e1.printStackTrace(new PrintWriter(sw));
			corticonlogger.error("[E00003]" + E00003 + "\n" + sw);
		}		
		return null;
	}

   呵呵,因为Freemarker的process默认是要写到一个文件当中.增加了IO的操作,比较慢,所以我的另外一篇文章也写了:

Freemarker输出字符串而不是文件

  或者参考如下的代码:

 

private String serviceAddress = "http://localhost:8080/axis/services/Corticon";
private URL webServiceUrl;
@PostConstruct
	private void init() {
		try {
			webServiceUrl = new URL(serviceAddress);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

	public DialogDecisionService() {
		// Get and config the connection.
		cfg.setClassForTemplateLoading(this.getClass(), "/");
		cfg.setObjectWrapper(new DefaultObjectWrapper());
		cfg.setNumberFormat("################");
		cfg.setCacheStorage(new freemarker.cache.MruCacheStorage(20, 250));
	}
public String processRequest(DialogDecisionInput input) {		
		try {			
			HttpURLConnection conn;
			conn = (HttpURLConnection) webServiceUrl.openConnection();

			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setRequestMethod("POST");
			conn.setUseCaches(false);
			conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
			conn.setRequestProperty("SOAPAction", "Corticon");			

			Map root = new HashMap();
			root.put("model", input);			

			Template template = cfg.getTemplate("soapRequest.ftl");
			/**
			 * StringWriter instead of Writer
			 * Class StringWriter contains a StringBuffer which can be rendered toString
			 * modify by heweiya
			 */
			OutputStream os = conn.getOutputStream();			
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "utf-8"));
			template.setEncoding("UTF-8");
			cfg.setDefaultEncoding("UTF-8");
			template.process(root, writer);			
			writer.flush();
			writer.close();			
			// Get the soap response.
			InputStream is = conn.getInputStream();
			String rc = IOUtils.toString(is);
			
			return rc;
		} catch (TemplateException e) {
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw));
			corticonlogger.error("[E00004]" + E00004 + "\n" + sw);
		} catch (IOException e1) {
			StringWriter sw = new StringWriter();
			e1.printStackTrace(new PrintWriter(sw));
			corticonlogger.error("[E00003]" + E00003 + "\n" + sw);
		}
		corticonlogger.error("[E00004]" + E00004 + "\n");
		return null;
	}

 发送的XML文件格式如下:

<?xml version="1.0" encoding="UTF-8"?>

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <SOAP-ENV:Body>

    <CorticonRequest xmlns="urn:Corticon" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" decisionServiceName="diabetes">

      <WorkDocuments>

        <D_diabetes id="D_diabetes_id_1">

          <currentPage>p_medInfo</currentPage>

          <dialogId>3</dialogId>

          <dialogInstanceId>771</dialogInstanceId>

          <displayTitle>Pediatric Diabetes Screening Tool</displayTitle>

          <function>screen</function>

				 <lastQuestionAnswered>q_sex</lastQuestionAnswered>

          <locale />

          <noOfChangedFlags>0</noOfChangedFlags>

          <productLine>Loan</productLine>

          <refId>D-00012</refId>

          <region>region 1</region>

          <sessionId xsi:nil="1" />

          <version>1</version>

	          

				           <p_medInfo id="P_medInfo_id_1">

				            <displayTitle>Diabetes Screening - Info</displayTitle>

				            <helpText></helpText>

				            <pageId>7</pageId>

				            <refId>D-00012</refId>

				            <version>1</version>

										            <q_sex id="Q_sex_id_1">										            	

										              <displayFlag>true</displayFlag>	              										              

										              <helpText></helpText>

										              <questionId>41</questionId>

										              <questionText>Sex</questionText>

										              <refId>D-00012</refId>

										              		<responseType>Custom</responseType>

										              		 <uiControl>Radio Button</uiControl>

										              <sequence>1.2</sequence>									             

										              <version>1</version>										              

										              <pageQuestionId>44</pageQuestionId>

										              		  	<response id="R_sex_id_13" Type="R_sex">												             

												              	<responseId>74</responseId>											               

												                <respEnabled xsi:nil="1" />

												                <respLabel>Female</respLabel>

												                <respSelected>false</respSelected>

												                <respSeq>3</respSeq>											                

												                <respValue>F</respValue>

												              </response>										              		  	

										              		  	<response id="R_sex_id_22" Type="R_sex">												             

												              	<responseId>73</responseId>											               

												                <respEnabled xsi:nil="1" />

												                <respLabel>Male</respLabel>

												                <respSelected>true</respSelected>

												                <respSeq>2</respSeq>											                

												                <respValue>M</respValue>

												              </response>										              		  	

										            </q_sex>										           

										            									            

										            								            

													        <q_medIntro id="Q_medIntro_id_7">												              

																  <displayFlag>true</displayFlag>																   

													              <helpText></helpText>

													              <questionId>39</questionId>

													              <questionText>We are asking a few extra questions in order to assess special risks in our new members.  I would like to get some additional information about your child.  This will just take a couple of minutes.</questionText>

													              <refId>D-00012</refId>

													              	 <responseType>No response</responseType>

													              

													              	 <uiControl xsi:nil="1" />

													              <version>1</version>

													              <sequence>1</sequence>

													              <pageQuestionId>40</pageQuestionId>

													              

															               <response id="R_medIntro_id_101" Type="R_medIntro">             

																			 <respEnabled xsi:nil="1" />

																			<respLabel xsi:nil="1" />

																			<respSelected xsi:nil="1" /><respValue></respValue>

															              </response>

													            </q_medIntro>													        

													        <q_age id="Q_age_id_8">												              

																  <displayFlag>true</displayFlag>																   

													              <helpText></helpText>

													              <questionId>40</questionId>

													              <questionText>Current Age</questionText>

													              <refId>D-00012</refId>

													              		<responseType>Custom</responseType>

													              

													              		 <uiControl>Text Box</uiControl>

													              <version>1</version>

													              <sequence>1.1</sequence>

													              <pageQuestionId>45</pageQuestionId>

													              

															               <response id="R_age_id_102" Type="R_age">           

																			 <respEnabled xsi:nil="1" />

																			<respLabel xsi:nil="1" />

																			<respSelected xsi:nil="1" /><respValue>0</respValue>

															              </response>

													            </q_age>													        

													        <q_weight id="Q_weight_id_9">												              

																  <displayFlag>true</displayFlag>																   

													              <helpText></helpText>

													              <questionId>42</questionId>

													              <questionText>Weight</questionText>

													              <refId>D-00012</refId>

													              		<responseType>Custom</responseType>

													              

													              		 <uiControl>Text Box</uiControl>

													              <version>1</version>

													              <sequence>1.3</sequence>

													              <pageQuestionId>43</pageQuestionId>

													              

															               <response id="R_weight_id_103" Type="R_weight">           

																			 <respEnabled xsi:nil="1" />

																			<respLabel xsi:nil="1" />

																			<respSelected xsi:nil="1" /><respValue>0</respValue>

															              </response>

													            </q_weight>													        

													        <q_heightFeet id="Q_heightFeet_id_10">												              

																  <displayFlag>true</displayFlag>																   

													              <helpText></helpText>

													              <questionId>43</questionId>

													              <questionText>Height - Feet</questionText>

													              <refId>D-00012</refId>

													              		<responseType>Custom</responseType>

													              

													              		 <uiControl>Text Box</uiControl>

													              <version>1</version>

													              <sequence>1.4</sequence>

													              <pageQuestionId>42</pageQuestionId>

													              

															               <response id="R_heightFeet_id_104" Type="R_heightFeet">           

																			 <respEnabled xsi:nil="1" />

																			<respLabel xsi:nil="1" />

																			<respSelected xsi:nil="1" /><respValue>0</respValue>

															              </response>

													            </q_heightFeet>													        

													       										              

																  <displayFlag>true</displayFlag>																   

													              <helpText></helpText>

													              <questionId>44</questionId>

													              <questionText>Height - Inches</questionText>

													              <refId>D-00012</refId>

													              		<responseType>Custom</responseType>

													              

													              		 <uiControl>Text Box</uiControl>

													              <version>1</version>

													              <sequence>1.5</sequence>

													              <pageQuestionId>41</pageQuestionId>

													              

															               <response id="R_heightInches_id_105" Type="R_heightInches">           

																			 <respEnabled xsi:nil="1" />

																			<respLabel xsi:nil="1" />

																			<respSelected xsi:nil="1" /><respValue>0</respValue>

															              </response>

													            </q_heightInches>													        

			          </p_medInfo>  

          

            <displayTitle></displayTitle>

            <helpText></helpText>

            <pageId>9</pageId>

            <refId>D-00012</refId>

            <version>1</version>

            <q_findingsIntro id="Q_findingsIntro_id_9">

           	  <displayFlag xsi:nil="1" />                                 

              <helpText></helpText>

              <questionId>50</questionId>

              <questionText>Recap:</questionText>

               <refId>D-00012</refId>

													              	 <responseType>No response</responseType>

													              

													              	 <uiControl xsi:nil="1" />

              <version>1</version>

              <sequence>1</sequence>

              <pageQuestionId>60</pageQuestionId>



	              <response id="R_findingsIntro_id_103" Type="R_findingsIntro">              

					 <respEnabled xsi:nil="1" />

					<respLabel xsi:nil="1" />

					<respSelected xsi:nil="1" />

	                <respValue></respValue>

	              </response>

            </q_findingsIntro>

            
          </p_findings>

        </D_diabetes>

      </WorkDocuments>

    </CorticonRequest>

  </SOAP-ENV:Body>

</SOAP-ENV:Envelope>
 

 

分享到:
评论

相关推荐

    实验-三、数据库安全性(目的、要求和模板).doc

    实验-三、数据库安全性(目的、要求和模板).doc

    基于Docker搭建K8s集群离线包

    基于Docker搭建K8s集群离线包,包含部署时所需的全部文件,可在内网环境中使用,K8s为1.23.0版本,docker为20.10.9-3版本

    基于springboot+vue实现的求职招聘类型网站源代码+数据库(优质毕设项目).zip

    基于springboot+vue实现的求职招聘类型网站源代码+数据库(优质毕设项目).zip个人经导师指导并认可通过的98分毕业设计项目,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者。也可作为课程设计、期末大作业。包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 基于springboot+vue实现的求职招聘类型网站源代码+数据库(优质毕设项目).zip个人经导师指导并认可通过的98分毕业设计项目,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者。也可作为课程设计、期末大作业。包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行! 基于springboot+vue实现的求职招聘类型网站源代码+数据库(优质毕设项目).zip个人经导师指导并认可通过的98分毕业设计项目,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者。也可作为课程设计、期末大作业。包含全部项目源码、该项目可以直接作为毕设使用。项目都经过严格调试,确保可以运行!基于springboot+vue实现的求

    基于Android系统Api封装常用工具类.zip

    基于Android系统Api封装常用工具类.zip

    基于PCA和SVM的人脸识别

    svm 基于PCA(主成分分析)和SVM(支持向量机)的人脸识别是一种常见的方法。这里是一个简要说明: PCA(主成分分析): PCA是一种降维技术,它通过线性变换将高维数据转换为低维数据,同时保留最大的数据方差。 在人脸识别中,PCA被用来提取人脸图像中的主要特征,从而减少数据的维度,并保留最重要的信息。 SVM(支持向量机): SVM是一种监督学习算法,用于分类和回归分析。 在人脸识别中,SVM被用来构建一个分类器,以将提取的人脸特征映射到相应的人脸身份标签。 基于PCA和SVM的人脸识别流程: 训练阶段: 收集训练数据集,包括多个人的人脸图像和相应的标签。 对每个人脸图像应用PCA,将其转换为低维特征向量。 使用这些特征向量训练一个SVM分类器,使其能够将人脸特征向量与相应的人脸标签关联起来。 测试阶段: 对待识别的人脸图像应用相同的PCA转换,将其转换为与训练数据相同的低维特征向量。 使用训练好的SVM分类器,将待识别的人脸特征向量与已知的人脸标签进行比较,从而确定其身份。 优点: PCA可以有效地降低数据的维度,减少计算复杂度,并提取最相关的特征。 SVM在处理

    天津科技大学-答辩通用PPT模板我给母校送模板作品.pptx

    PPT模板,答辩PPT模板,毕业答辩,学术汇报,母校模板,我给母校送模板作品,周会汇报,开题答辩,教育主题模板下载。PPT素材下载。

    Java SE Development Kit 11.0.23 macOS x64 DMG Installer

    Java SE Development Kit 11.0.23 macOS x64 DMG Installer

    课设&大作业-SSM毕业设计项目.zip

    【资源说明】【毕业设计】 1、该资源内项目代码都是经过测试运行成功,功能正常的情况下才上传的,请放心下载使用。 2、适用人群:主要针对计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、数学、电子信息等)的同学或企业员工下载使用,具有较高的学习借鉴价值。 3、不仅适合小白学习实战练习,也可作为大作业、课程设计、毕设项目、初期项目立项演示等,欢迎下载,互相学习,共同进步!

    studyopencv2

    studyopencv2

    实验五-使用matlab实现卷积的运算.pdf

    实验五-使用matlab实现卷积的运算.pdf

    2024年中国纸杯蛋糕盒行业研究报告.docx

    2024年中国纸杯蛋糕盒行业研究报告

    总结作图常用的操作 Python数据分析及可视化-Matplotlib极简入门.zip

    matplotlib绘图 通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图、直方图、功率谱、条形图、错误图、散点图等。 Matplotlib基础知识 1.Matplotlib中的基本图表包括的元素 x轴和y轴 水平和垂直的轴线 x轴和y轴刻度 刻度标示坐标轴的分隔,包括最小刻度和最大刻度 x轴和y轴刻度标签 表示特定坐标轴的值 绘图区域 实际绘图的区域 2.hold属性 hold属性默认为True,允许在一幅图中绘制多个曲线;将hold属性修改为False,每一个plot都会覆盖前面的plot。 但是不推荐去动hold这个属性,这种做法(会有警告)。因此使用默认设置即可。 3.网格线 grid方法 使用grid方法为图添加网格线 设置grid参数(参数与plot函数相同) .lw代表linewidth,线的粗细 .alpha表示线的明暗程度 4.axis方法 如果axis方法没有任何参数,则返回当前坐标轴的上下限 5.xlim方法和ylim方法 除了plt.axis方法,还可以通过xlim,ylim方法设置坐标轴范围

    java本科毕业设计基于RFID技术的国有资产管理系统源码后台项目.zip

    高分设计源码,详情请查看资源内容中使用说明 高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明高分设计源码,详情请查看资源内容中使用说明

    springboot部署系统(自动化部署+缓存管理+业务降级+应用监控).zip

    springboot框架 一、Spring Boot基础应用 Spring Boot特征 概念: 约定优于配置,简单来说就是你所期待的配置与约定的配置一致,那么就可以不做任何配置,约定不符合期待时才需要对约定进行替换配置。 特征: 1. SpringBoot Starter:他将常用的依赖分组进行了整合,将其合并到一个依赖中,这样就可以一次性添加到项目的Maven或Gradle构建中。 2,使编码变得简单,SpringBoot采用 JavaConfig的方式对Spring进行配置,并且提供了大量的注解,极大的提高了工作效率,比如@Configuration和@bean注解结合,基于@Configuration完成类扫描,基于@bean注解把返回值注入IOC容器。 3.自动配置:SpringBoot的自动配置特性利用了Spring对条件化配置的支持,合理地推测应用所需的bean并自动化配置他们。 4.使部署变得简单,SpringBoot内置了三种Servlet容器,Tomcat,Jetty,undertow.我们只需要一个Java的运行环境就可以跑SpringBoot的项目了

    CUDA规约求和.cu

    CUDA规约求和.cu

    工程有限公司计算机信息系统数据信息安全管理办法.doc

    工程有限公司计算机信息系统数据信息安全管理办法.doc

    小学教育信息化工作计划.doc

    小学教育信息化工作计划.doc

    springboot停车位管理源码.rar

    springboot停车位管理源码.rarspringboot停车位管理源码.rarspringboot停车位管理源码.rar

    实验七-管道通信.doc

    实验七-管道通信.doc

    基于YOLOv5行人车辆跟踪检测识别计数系统源码.zip

    YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

Global site tag (gtag.js) - Google Analytics