首页 » Web前端 » phpcurlwsdl技巧_Spring整合CXF 宣告webservice接口做事器和客户端WSDL简单解析

phpcurlwsdl技巧_Spring整合CXF 宣告webservice接口做事器和客户端WSDL简单解析

访客 2024-12-05 0

扫一扫用手机浏览

文章目录 [+]

下面开始,这里Spring用的4.2的,CXF用的3.2的,JDK是1.8的,这里只假如1.6及往后的就没问题了,紧张是要用javax.jws的包

做事端webservice接口

phpcurlwsdl技巧_Spring整合CXF 宣告webservice接口做事器和客户端WSDL简单解析

package com.lovo.webservice;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;import javax.jws.soap.SOAPBinding.Style;/ SOAP协议的webservice接口 @author WZH 做事器端,做事供应者 // @WebService 标记为一个webservice接口 1.targetNamespace:指定你想要的名称空间,默认认是利用接口实现类的包名的反缀, 但是在实际测试中创造cxf+spring编写webservice的时候,targetNamespace要指向 做事类的接口所在的包名而不是实现类的包名,否则,在客户端将不能识别可用的做事 2.serviceName: 对外发布的做事名,指定 Web Service 的做事名称:wsdl:service。
缺省值为 Java 类的大略名称 + Service。
(字符串) 3.portName: wsdl:portName。
缺省值为 WebService.name+Port。
@SOAPBinding 绑定, 这个标记怎么说,我这里测试的时候加不加都无所谓,但是有同仁说过 不加这个有时候会涌现客户端无法识别,有的人又报错,以是还是加上吧 Exception in thread "main" com.sun.xml.internal.ws.model.RuntimeModelerException: runtime modeler error: Wrapper class org.soap.service.jaxws.Add is not found. Have you run APT to generate them? /@WebService(targetNamespace ="http://webservice.lovo.com/",serviceName="HelloService",portName="HellowServicePort")@SOAPBinding(style=Style.RPC)public interface IHelloService { / @WebResult 注释用于定制从返回值至 WSDL 部件或 XML 元素的映射。
将此注释运用于客户机或做事器做事端点接口(SEI)上的方法, 或者运用于 JavaBeans 端点的做事器端点实现类 1.name:当返回值列示在 WSDL 文件中并且在连接上的中找到该返回值时,指定该返回值的名称。
对付 RPC 绑定,这是用于表示返回值的 2.targetNamespace:指定返回值的 XML 名称空间。
仅当操作类型为 RPC 或者操作是文档类型并且参数类型为 BARE 时才利用此参数。
(字符串) @WebMethod 注释表示作为一项 Web Service 操作的方法,将此注释运用于客户机或做事器做事端点接口(SEI)上的方法, 或者运用于 JavaBeans 端点的做事器端点实现类。
1.operationName:指定与此方法相匹配的wsdl:operation 的名称。
缺省值为 Java 方法的名称。
(字符串) @WebParam 注释用于定制从单个参数至 Web Service 部件和 XML 元素的映射 1.name :参数的名称。
如果操作是远程过程调用(RPC)类型并且未指定partName 属性, 那么这是用于表示参数的 wsdl:part 属性的名称。
如果操作是文档类型或者参数映射至某个头,那么 -name 是用于表示该参数的 XML 元素的局部名称。
如果操作是文档类型、参数类型为 BARE 并且办法为 OUT 或 INOUT,那么必须指定此属性。
(字符串) / @WebResult(name="helloRequest",targetNamespace="http://webservice.lovo.com/") @WebMethod(operationName="sayHello1") public String hello(@WebParam(name="msg")String message);}

做事端接口实现类

phpcurlwsdl技巧_Spring整合CXF 宣告webservice接口做事器和客户端WSDL简单解析
(图片来自网络侵删)

package com.lovo.webservice.impl;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;import javax.jws.soap.SOAPBinding.Style;import org.springframework.stereotype.Service;import com.lovo.webservice.IHelloService;/ endpointInterface: 做事接口全路径, 指定做SEI(Service EndPoint Interface)做事端点接口,大略的说就写接口的全路径 /@WebService(endpointInterface="com.lovo.webservice.IHelloService")@SOAPBinding(style=Style.RPC)public class HelloServiceImpl implements IHelloService { @Override public String hello(String message) { return "你好" + message; }}

前面的两段代码只是大略的java加上一些表明,下面把代码整合入Spring的web工程web.xml 配置需在web.xml中配置CXF

<!-- 启动CXF框架 web service--> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/webservice/</url-pattern> </servlet-mapping>

完全的xml文件:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Spring</display-name> <context-param> <param-name>webAppRootKey</param-name> <param-value>Spring.root</param-value> </context-param><!-- spring容器启动 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/log4j.xml</param-value> </context-param> <!-- 指定日志刷新的韶光,单位毫秒 --> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener><!-- 配置欢迎页面 --><welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 字符过滤器 --><filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping><!-- <filter> <filter-name>encoding</filter-name> <filter-class>com.lovo.filter.EncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/</url-pattern> </filter-mapping> --> <!-- 启动CXF框架 web service--> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/webservice/</url-pattern> </servlet-mapping> <!-- 配置核心掌握器 --><servlet> <servlet-name>Spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

配置cxf的配置文件,这个也可以直接配置在Spring的配置文件中,看项目风格吧

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!--CXF配置 这里由于是3.x的CXF 只须要引用一个文件就好--> <import resource="classpath:META-INF/cxf/cxf.xml" /> <!-- id是接口名,不重复就好,serviceClass 是接口的路径,address是对外暴露的地址名,bean classs是引用的接口的实现类 --> <jaxws:server id="helloService" serviceClass="com.lovo.webservice.IHelloService" address="/HelloService"> <jaxws:serviceBean> <bean class="com.lovo.webservice.impl.HelloServiceImpl"></bean> </jaxws:serviceBean> </jaxws:server> </beans>

在application.xml中引入cxf.xml

<!--CXF配置 --><import resource="classpath:META-INF/cxf/cxf.xml"/><import resource="classpath:/cxf-beans.xml"/>

完全的application.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 开启自动扫描机制 --> <context:component-scan base-package="com"></context:component-scan> <!-- <context:property-placeholder location="classpath:mysql.properties"/> --> <!-- 配置数据库连接池 --> <!-- 商业数据源 c3p0, apache供应的DBCP --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property> <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:oracle"></property> <property name="user" value="system"></property> <property name="password" value="Password01"></property> <property name="initialPoolSize" value="15"></property><!-- 初始化连接数的数量 --> <property name="minPoolSize" value="15"></property> <property name="maxPoolSize" value="100"></property> <property name="acquireIncrement" value="5"></property> <property name="maxStatements" value="200"></property> <property name="maxIdleTime" value="200"></property><!-- 超时时间 --> <property name="checkoutTimeout" value="2000"></property><!-- 多永劫光回收一次超时连接 --> </bean> <!-- SessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 如果有多包须要取种别号的话,利用,或;分割 --> <property name="typeAliasesPackage" value="com.lovo.beans"></property> </bean> <!-- 将Session与Mapper直接进行关联 --> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.lovo.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 配置事务管理器 --> <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- session实例化 --> <!-- 声明事物 --> <tx:advice id="txAdvice" transaction-manager="txManage"> <tx:attributes> <tx:method name="" read-only="true"></tx:method> <tx:method name="add" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method> <tx:method name="save" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method> <tx:method name="insert" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method> <tx:method name="update" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method> <tx:method name="modify" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method> <tx:method name="change" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" isolation="REPEATABLE_READ"></tx:method> <tx:method name="delete" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method> <tx:method name="remove" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method> <!-- 添加了一个删除方法的命名 --> <tx:method name="del" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method> <tx:method name="select" read-only="true" propagation="SUPPORTS"></tx:method> <tx:method name="find" read-only="true" propagation="SUPPORTS"></tx:method> <tx:method name="get" read-only="true" propagation="SUPPORTS"></tx:method> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="serviceMethods" expression="execution( com.lovo.service.impl..(..))"></aop:pointcut> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"></aop:advisor> </aop:config> <!-- 开启切面功能 --> <aop:aspectj-autoproxy /> <!--CXF配置 --> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:/cxf-beans.xml"/></beans>

下面来测试下发布是否成功,我这里是tomcat 工程名是Spring,拦截的是webservice,以是访问的地址是:http://localhost:8080/Spring/webservice?wsdl

涌现界面

右键连接地址可以下载wsdl

这样就发布了一个大略的webservice接口,下面在发布一个接口以便wsdl的时候做比拟,然后再发布一个rest风格的接口

package com.lovo.webservice;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;import javax.jws.soap.SOAPBinding.Style;/ SOAP协议的webservice接口 @author WZH 做事器端,做事供应者 // @WebService 标记为一个webservice接口 1.targetNamespace:指定你想要的名称空间,默认认是利用接口实现类的包名的反缀, 但是在实际测试中创造cxf+spring编写webservice的时候,targetNamespace要指向 做事类的接口所在的包名而不是实现类的包名,否则,在客户端将不能识别可用的做事 2.serviceName: 对外发布的做事名,指定 Web Service 的做事名称:wsdl:service。
缺省值为 Java 类的大略名称 + Service。
(字符串) 3.portName: wsdl:portName。
缺省值为 WebService.name+Port。
@SOAPBinding 绑定, 这个标记怎么说,我这里测试的时候加不加都无所谓,但是有同仁说过 不加这个有时候会涌现客户端无法识别,有的人又报错,以是还是加上吧 Exception in thread "main" com.sun.xml.internal.ws.model.RuntimeModelerException: runtime modeler error: Wrapper class org.soap.service.jaxws.Add is not found. Have you run APT to generate them? /@WebService(targetNamespace ="http://webservice.lovo.com/",serviceName="HelloService",portName="HellowServicePort")@SOAPBinding(style=Style.RPC)public interface IHelloService { / @WebResult 注释用于定制从返回值至 WSDL 部件或 XML 元素的映射。
将此注释运用于客户机或做事器做事端点接口(SEI)上的方法, 或者运用于 JavaBeans 端点的做事器端点实现类 1.name:当返回值列示在 WSDL 文件中并且在连接上的中找到该返回值时,指定该返回值的名称。
对付 RPC 绑定,这是用于表示返回值的 2.targetNamespace:指定返回值的 XML 名称空间。
仅当操作类型为 RPC 或者操作是文档类型并且参数类型为 BARE 时才利用此参数。
(字符串) @WebMethod 注释表示作为一项 Web Service 操作的方法,将此注释运用于客户机或做事器做事端点接口(SEI)上的方法, 或者运用于 JavaBeans 端点的做事器端点实现类。
1.operationName:指定与此方法相匹配的wsdl:operation 的名称。
缺省值为 Java 方法的名称。
(字符串) @WebParam 注释用于定制从单个参数至 Web Service 部件和 XML 元素的映射 1.name :参数的名称。
如果操作是远程过程调用(RPC)类型并且未指定partName 属性, 那么这是用于表示参数的 wsdl:part 属性的名称。
如果操作是文档类型或者参数映射至某个头,那么 -name 是用于表示该参数的 XML 元素的局部名称。
如果操作是文档类型、参数类型为 BARE 并且办法为 OUT 或 INOUT,那么必须指定此属性。
(字符串) / @WebResult(name="helloRequest",targetNamespace="http://webservice.lovo.com/") @WebMethod(operationName="sayHello1") public String hello(@WebParam(name="msg")String message); @WebResult(name="loginRequest",targetNamespace="http://webservice.lovo.com/") @WebMethod(operationName="login") public String login(@WebParam(name="userName")String userName,@WebParam(name="password")String password);}

这里须要特殊把稳,targetNamespace中路径结尾需写反斜杠/不然会涌现问题

package com.lovo.webservice.impl;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;import javax.jws.soap.SOAPBinding.Style;import org.springframework.stereotype.Service;import com.lovo.webservice.IHelloService;/ endpointInterface: 做事接口全路径, 指定做SEI(Service EndPoint Interface)做事端点接口,大略的说就写接口的全路径 /@WebService(endpointInterface="com.lovo.webservice.IHelloService")@SOAPBinding(style=Style.RPC)public class HelloServiceImpl implements IHelloService { @Override public String hello(String message) { return "你好" + message; } @Override public String login(String userName, String password) { String req = null; //这里做个假的登录验证 if("小明".equals(userName)&&"123456".equals(password)) { req = "登录成功"; }else { req = "登录失落败"; } return req; }}

这个时候再访问wsdl的网址就瞥见有两个方法了

下载wsdl

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://webservice.lovo.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloService" targetNamespace="http://webservice.lovo.com/"> <wsdl:message name="loginResponse"> <wsdl:part name="loginRequest" type="xsd:string"> </wsdl:part> </wsdl:message> <wsdl:message name="sayHello1Response"> <wsdl:part name="helloRequest" type="xsd:string"> </wsdl:part> </wsdl:message> <wsdl:message name="sayHello1"> <wsdl:part name="msg" type="xsd:string"> </wsdl:part> </wsdl:message> <wsdl:message name="login"> <wsdl:part name="userName" type="xsd:string"> </wsdl:part> <wsdl:part name="password" type="xsd:string"> </wsdl:part> </wsdl:message> <wsdl:portType name="IHelloService"> <wsdl:operation name="sayHello1"> <wsdl:input message="tns:sayHello1" name="sayHello1"> </wsdl:input> <wsdl:output message="tns:sayHello1Response" name="sayHello1Response"> </wsdl:output> </wsdl:operation> <wsdl:operation name="login"> <wsdl:input message="tns:login" name="login"> </wsdl:input> <wsdl:output message="tns:loginResponse" name="loginResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="HelloServiceSoapBinding" type="tns:IHelloService"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="sayHello1"> <soap:operation soapAction="" style="rpc"/> <wsdl:input name="sayHello1"> <soap:body namespace="http://webservice.lovo.com/" use="literal"/> </wsdl:input> <wsdl:output name="sayHello1Response"> <soap:body namespace="http://webservice.lovo.com/" use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="login"> <soap:operation soapAction="" style="rpc"/> <wsdl:input name="login"> <soap:body namespace="http://webservice.lovo.com/" use="literal"/> </wsdl:input> <wsdl:output name="loginResponse"> <soap:body namespace="http://webservice.lovo.com/" use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="HelloService"> <wsdl:port binding="tns:HelloServiceSoapBinding" name="HellowServicePort"> <soap:address location="http://localhost:8080/Spring/webservice/HelloService"/> </wsdl:port> </wsdl:service></wsdl:definitions>

阅读wsdl的方法详细的阅读办法办法可以看这篇文章:http://blog.csdn.net/liuchunming033/article/details/41210151

拿到一个wsdl,个人一样平常是先看末了,也便是wsdl:service部分。
wsdl:service:存放接口名和接口地址

<!--多个接口放在一起为一个service name为视图做事名--> <wsdl:service name="HelloService"> <!-- 一个port表示一个接口 name表示刚刚绑定的name,便是portName这个参数 --> <wsdl:port binding="tns:HelloServiceSoapBinding" name="HellowServicePort"> <!-- address表示接口工具的地址 --> <soap:address location="http://localhost:8080/Spring/webservice/HelloService"/> </wsdl:port> </wsdl:service>

然后再看binding部分,也便是wsdl:bindingwsdl:binding:每个端口定义格式和协议细节

<!-- 把接口进行soap绑定 type为接口名 name对应 service中的binding参数--> <wsdl:binding name="HelloServiceSoapBinding" type="tns:IHelloService"> <!--指明绑定的协议为http soap协议,类型为rpc 这里便是@SOAPBinding那个表明指定了为rpc ,如果不指定默认为document--> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <!--详细方法的bnding类型,接口中有多少个方法就有多少个operation 个中name表示方法名 literal文本--> <wsdl:operation name="sayHello1"> <soap:operation soapAction="" style="rpc"/> <wsdl:input name="sayHello1"> <!--方法的命名空间namespace 便是方法@WebResult表明中指定的targetNamespace--> <soap:body namespace="http://webservice.lovo.com/" use="literal"/> </wsdl:input> <wsdl:output name="sayHello1Response"> <soap:body namespace="http://webservice.lovo.com/" use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="login"> <soap:operation soapAction="" style="rpc"/> <wsdl:input name="login"> <soap:body namespace="http://webservice.lovo.com/" use="literal"/> </wsdl:input> <wsdl:output name="loginResponse"> <soap:body namespace="http://webservice.lovo.com/" use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding>

再看portType部分,wsdl:portTypewsdl:portType:描述webservice可被实行的操作,以及干系的,通过binding指向portType

<!--name表示接口名--> <wsdl:portType name="IHelloService"> <!--接口中有很多方法,没个opertion就表示一个方法名,name为方法名,为代码中@WebMethod中定义的operationName--> <wsdl:operation name="sayHello1"> <!--通过message可以查看方法中对应的参数,input表示传入,output表示返回--> <wsdl:input message="tns:sayHello1" name="sayHello1"> </wsdl:input> <wsdl:output message="tns:sayHello1Response" name="sayHello1Response"> </wsdl:output> </wsdl:operation> <wsdl:operation name="login"> <wsdl:input message="tns:login" name="login"> </wsdl:input> <wsdl:output message="tns:loginResponse" name="loginResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType>

在看message,wsdl:messagewsdl:message:定义一个操作(方法)的数据参数(可有多个参数)

<!--login方法的返回值定义,一样平常为方法名+response 这里方法只有一个大略的返回值,类型为String--> <wsdl:message name="loginResponse"> <wsdl:part name="loginRequest" type="xsd:string"> </wsdl:part> </wsdl:message> <wsdl:message name="sayHello1Response"> <wsdl:part name="helloRequest" type="xsd:string"> </wsdl:part> </wsdl:message> <wsdl:message name="sayHello1"> <wsdl:part name="msg" type="xsd:string"> </wsdl:part> </wsdl:message> <!--name对应portType中的operation name属性对应的值。
login方法好传入的参数,有几个参数就有几个part--> <wsdl:message name="login"> <!--参数名为userName,参数类型为String--> <wsdl:part name="userName" type="xsd:string"> </wsdl:part> <wsdl:part name="password" type="xsd:string"> </wsdl:part> </wsdl:message>

末了还是须要看看开始部分

<!--name 表示接口名,targetNamespace表示接口的命名空间--><?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://webservice.lovo.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloService" targetNamespace="http://webservice.lovo.com/">

这么一个大略的xml就解析完了完全的xml文档

<!--name 表示接口名,targetNamespace表示接口的命名空间--><?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://webservice.lovo.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloService" targetNamespace="http://webservice.lovo.com/"> <!--login方法的返回值定义,一样平常为方法名+response 这里方法只有一个大略的返回值,类型为String--> <wsdl:message name="loginResponse"> <wsdl:part name="loginRequest" type="xsd:string"> </wsdl:part> </wsdl:message> <wsdl:message name="sayHello1Response"> <wsdl:part name="helloRequest" type="xsd:string"> </wsdl:part> </wsdl:message> <wsdl:message name="sayHello1"> <wsdl:part name="msg" type="xsd:string"> </wsdl:part> </wsdl:message> <!--name对应portType中的operation name属性对应的值。
login方法好传入的参数,有几个参数就有几个part--> <wsdl:message name="login"> <!--参数名为userName,参数类型为String--> <wsdl:part name="userName" type="xsd:string"> </wsdl:part> <wsdl:part name="password" type="xsd:string"> </wsdl:part> </wsdl:message> <!--name表示接口名--> <wsdl:portType name="IHelloService"> <!--接口中有很多方法,没个opertion就表示一个方法名,name为方法名,为代码中@WebMethod中定义的operationName--> <wsdl:operation name="sayHello1"> <!--通过message可以查看方法中对应的参数,input表示传入,output表示返回--> <wsdl:input message="tns:sayHello1" name="sayHello1"> </wsdl:input> <wsdl:output message="tns:sayHello1Response" name="sayHello1Response"> </wsdl:output> </wsdl:operation> <wsdl:operation name="login"> <wsdl:input message="tns:login" name="login"> </wsdl:input> <wsdl:output message="tns:loginResponse" name="loginResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> <!-- 把接口进行soap绑定 type为接口名 name对应 service中的binding参数--> <wsdl:binding name="HelloServiceSoapBinding" type="tns:IHelloService"> <!--指明绑定的协议为http soap协议,类型为rpc 这里便是@SOAPBinding那个表明指定了为rpc ,如果不指定默认为document--> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <!--详细方法的bnding类型,接口中有多少个方法就有多少个operation 个中name表示方法名 literal文本--> <wsdl:operation name="sayHello1"> <soap:operation soapAction="" style="rpc"/> <wsdl:input name="sayHello1"> <!--方法的命名空间namespace 便是方法@WebResult表明中指定的targetNamespace--> <soap:body namespace="http://webservice.lovo.com/" use="literal"/> </wsdl:input> <wsdl:output name="sayHello1Response"> <soap:body namespace="http://webservice.lovo.com/" use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="login"> <soap:operation soapAction="" style="rpc"/> <wsdl:input name="login"> <soap:body namespace="http://webservice.lovo.com/" use="literal"/> </wsdl:input> <wsdl:output name="loginResponse"> <soap:body namespace="http://webservice.lovo.com/" use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <!--多个接口放在一起为一个service name为视图做事名--> <wsdl:service name="HelloService"> <!-- 一个port表示一个接口 name表示刚刚绑定的name,便是portName这个参数 --> <wsdl:port binding="tns:HelloServiceSoapBinding" name="HellowServicePort"> <!-- address表示接口工具的地址 --> <soap:address location="http://localhost:8080/Spring/webservice/HelloService"/> </wsdl:port> </wsdl:service></wsdl:definitions>

wsdl阅读完成了,须要的参数都明白了,下面就可以做个大略的客户端进行调用,webService客户端调用的方法有很多,我们这里大略的做几个。
一:Service编程办法特点:可以自定义关键元素,方便往后掩护,是一种标准的开拓办法首先写一个客户端调用工具

package com.lovo.client;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;import javax.jws.soap.SOAPBinding.Style;/ 客户端调用者,根据wsdl初始化接口工具 @author WZH targetNamespace 对应wsdl举头 targetNamespace,serviceName和portName分别对应wsdl结尾的wsdl:service name="HelloService" 和wsdl:port binding="tns:HelloServiceSoapBinding" name="HellowServicePort"> 由于wsdl文档中有指定SOAP类型,以是需书写类型 <soap:operation soapAction="" style="rpc"/> /@WebService(targetNamespace="http://webservice.lovo.com/",serviceName="HelloService",portName="HellowServicePort")@SOAPBinding(style=Style.RPC)public interface HellowService { / 方法名对应wsdl wsdl:operation name="login" @param username 参数名对应 wsdl:part name="userName" type="xsd:string" @param password @return 方法名映射 <wsdl:operation name="login"> 地址映射<soap:body namespace="http://webservice.lovo.com" use="literal"/> 参数映射:<wsdl:part name="userName" type="xsd:string"> <wsdl:part name="password" type="xsd:string"> / @WebResult(name="login",targetNamespace="http://webservice.lovo.com/") @WebMethod(operationName = "login") public String login(@WebParam(name = "userName")String username , @WebParam(name = "password")String password); @WebResult(name="helloRequest",targetNamespace="http://webservice.lovo.com/") @WebMethod(operationName="sayHello1") public String hello(@WebParam(name="msg")String msg); }

这里须要特殊把稳,targetNamespace中路径结尾需写反斜杠/不然会涌现问题

准备事情做好了,可以做个客户真个调用了

package com.lovo.client;import java.net.URL;import javax.xml.namespace.QName;import javax.xml.ws.Service;import javax.xml.ws.soap.SOAPBinding;public class wsClient { / XML 类 第一个参数表示地址,第二个参数表示端口名 这里第一个参数是wsdl 表头targetNamespace 第二个参数是service name / private static final QName SERVICE_NAME = new QName("http://webservice.lovo.com/", "HelloService"); private static final QName PORT_NAME = new QName("http://webservice.lovo.com/", "HellowServicePort"); //编程办法一 begin ----------------------- public static void testSayHello() { try { //创建Serivce业务类 Service service = Service.create(SERVICE_NAME); //连接地址 <soap:address location="http://localhost:8080/Spring/webservice/HelloService"/> String endPointAddress = "http://localhost:8080/Spring/webservice/HelloService"; //配置业务访问参数 portName, bindingId, endpointAddress, //这里由于是SOPA11的所有配置11,SOAP现在有11和12两种 可通过xmlns:ns1="http://schemas.xmlsoap.org/soap/http" 判断 service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endPointAddress); //把本地客户真个接口与网络上的WSDL连接起来 HellowService hs = service.getPort(HellowService.class); System.out.println(hs.hello("小明")); } catch (Exception e) { e.printStackTrace(); } } //end------------------------------------- //编程办法二 begin 不知道为什么 Service.create报错,可能是jar缘故原由----------------------- public static void testSayHello2() { try { //创建WSDL的URL,把稳不是做事地址 URL url = new URL("http://localhost:8080/Spring/webservice?wsdl"); //创建做事名,代码有写成了静态常量,这里直接引用 //创建做事视图 javax.xml.ws.Service service = Service.create(url, SERVICE_NAME); //获取做事实例 HellowService hs = service.getPort(new QName("http://webservice.lovo.com/", "HellowServicePort"), HellowService.class); System.out.println(hs.hello("小明")); } catch (Exception e) { e.printStackTrace(); } } //end------------------------------------- public static void main(String[] args) { //testSayHello(); testSayHello2(); }}

剩下的等有空的再完善

标签:

相关文章

php笔试mysql技巧_MySQL面试题

关系型数据库的特点包括:表格构造:数据以表格形式存储,表由行(记录)和列(字段)组成。每行代表一个数据项,而列包含该项的属性。数据...

Web前端 2024-12-07 阅读0 评论0