如何根据WSDL文档开发部署Web Service服务?
WSDL文档是描述Web Service接口该如何开发和部署的具体描述文档,十分重要。一个接口的WSDL文档里会包含接口部署时的服务名(service name),接口的绑定信息,绑定端口信息和接口方法中的参数信息等。根据这些信息,我们可以通过WSDL文档反向自动建立接口的java代码结构,提高开发工作效率。
下面介绍一下使用WSDL文档反向自动建立接口的java代码结构的步骤。
一、新建Dynamic Web Project 项目。
二、在项目中添加已有的WSDL文档。
WSDL文档直接放在项目根目录下。
WSDL文档内容说明:
1、在项目上右键,进入新建web service配置窗口。
2、选择Web Service,Next。
3、配置web service
包括创建web service用哪种类型、选择一个wsdl文档、配置tomcat和web service运行环境以及service所在项目、webservice处于哪个进度(选develop阶段)。
其中service implementations选择我们在步骤(三.1)中导入的wsdl文件。
Web service type选项栏中有两个可选项,分别是 Bottom up Java bean Web Service和Top down Java bean Web Service。前者表示根据已有的JAVA文件生成WSDL文档和web service服务,后者表示根据已有的WSDL文档反向生成web service服务的JAVA代码结构。
在这里,我们选择后者。
4、接下来就都是next,最后finish。web service代码结构已经生成了。
接下来的工作,就是在IfAPServiceSoapBindingImpl.java文件中编写我们的业务逻辑代码,可以另外创建其他package包,合理安排代码模块。
四、web service的业务代码编写完之后,开始发布。
自动发布的方法,就是在项目的WEB-INF路径下新建一个server-config.wsdd发布文件。这个文件的大部分内容可以来源于模板(模板内容稍后贴出),只有<service>...</service>部分填写刚写好的web service的发布部署信息。这部分信息在自动生成的deploy.wsdd中已经有了,我们可以直接拿过来,复制到server-config.wsdd中。
<?xml version="1.0"encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<globalConfiguration>
<parameter name="adminPassword" value=XXXX" />
<"attachments.Directory" "D:\Tomcat\temp\axis\WEB-INF\attachments"/>
<"attachments.implementation"
"org.apache.axis.attachments.AttachmentsImpl"/>
<"sendxsitypes" "true" />
<"sendMultiRefs" "sendXMLDeclaration" "axis.sendMinimizedElements" requestFlow>
<handler type="java:org.apache.axis.handlers.JWSHandler">
<"scope" "session"/>
</handler>
<"request"/>
<"extension" ".jwr"/>
</handler>
</requestFlow>
</globalConfiguration>
<"LocalResponder"
"java:org.apache.axis.transport.local.LocalResponder"/>
<"URLMapper" "java:org.apache.axis.handlers.http.URLMapper"/>
<"Authenticate"
"java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
<service "AdminService" provider="java:MSG">
<"allowedMethods" "AdminService" />
<"enableRemoteAdmin" "false" />
<"className" "org.apache.axis.utils.Admin"/>
<namespace>http://xml.apache.org/axis/wsdd/</namespace>
</service>
<!-- <service name="Version"provider="java:RPC"> -->
<!-- <parameter name="allowedMethods"value="getVersion" /> -->
<!-- <parameter name="className"value="org.apache.axis.Version" /> -->
<!-- </service> -->
<!--myself webservice-->
<"IfAPService" "java:RPC" style="rpc"use="encoded">
<"wsdlTargetNamespace" "http://eaaapi.eaa.eidc.huawei.com/"/>
<"wsdlServiceElement" "IfAPService"/>
<"wsdlServicePort" "com.huawei.eidc.eaa.eaaapi.IfAPServiceSoapBindingSkeleton"/>
<"wsdlPortType" "APService"/>
<"typeMappingVersion" "1.2"/>
<"*"/>
</service>
<transport "http">
<"URLMapper" />
<"java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
</requestFlow>
<"qs:list"
"org.apache.axis.transport.http.QSListHandler"/>
<"qs.list"
"qs:method"
"org.apache.axis.transport.http.QSMethodHandler"/>
<"qs.method"
"qs:wsdl"
"org.apache.axis.transport.http.QSWSDLHandler"/>
<"qs.wsdl"
"org.apache.axis.transport.http.QSWSDLHandler"/>
</transport>
<"local">
<responseFlow>
<"LocalResponder" />
</responseFlow>
</transport>
</deployment>
2、发布web service。
在上述步骤之后,我们可以发布接口项目了。只需要将项目打包成war文件,并将war包放到指定的tomcat的webapps路径下,启动tomcat。这里我们把打包的包名设为webTest.war
3、查看发布的web service是否成功。
在浏览器中输入: http://localhost:8080/webTest/services
(注意不要在”services”后面加斜杠‘/ ’),得到下面的结果,可知web service发布成功。
五、测试代码
import java.io.IOException;
import javax.xml.namespace.QName;
import model.Utility;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.params.CoreConnectionPNames;
/**模拟ADC向SI发送数据请求*/
public class ADC2SI {
private static String test_url = "http://172.0.0.1:8080/webTest/services/IfAPService";
//调用接口,发送post请求
public static void main(String[] args) throws HttpException, IOException{
String str = "params";
postmethod2(test_url,str);
}
public static void postmethod2(String url, String str) throws HttpException, IOException{
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getoptions();
指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(url);
options.setTo(targetEPR);
指定要调用的sayHelloToPerson方法及WSDL文件的命名空间QName opAddEntry = new QName("http://eaaapi.eaa.eidc.huawei.com","corpBinding");
指定方法的参数值Object[] opAddEntryArgs = new Object[] { str };
方法返回值的数据类型的Class对象
@SuppressWarnings("rawtypes")
Class[] classes = new Class[] { String.class };
方法并输出该方法的返回值try{
System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);
System.out.println("发送数据 SUCCESS");
}catch(Exception e){
System. Failed");}}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。