使用 Apache CXF 实现 Web Service 详解
<importresource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<importresource="classpath:META-INF/cxf/cxf-servlet.xml" />
?
<bean id="helloService"/>
<!--
<bean id="helloService"/>
-->
<!--endpoint?方式发布web服务和 server方式一样 -->
<!--
<jaxws:endpointid="helloServiceWs" address="/helloService"
???? implementor="#helloService"/>
-->
<!--
?????另一种写法,建议不要用这种方法 ,如果实现类有的属性要通过spring依赖注入的话,
?????这种方法只是简单的new个实现类,他的属性没有通过spring依赖注入给注入值
-->
<!--
<jaxws:endpointid="helloServiceWs" address="/helloService"
???? implementor="com.jaxb.service.HelloServiceImpl"/>
-->
?
<!—下面这个是wss4j的配置,后面会讲到相关知识,需要配置在spring配置文件中 -->
<!--wss4j?服务端配置 -->
<bean id="wss4jInInterceptor"
value="UsernameToken" />
???????????? <!--<entry key="passwordType" value="PasswordText" />-->
???????????? <!--密码使用MD5密文发送 -->
???????????? <entrykey="passwordDigest" value="PasswordText" />
???????????? <entrykey="passwordCallbackClass"
???????????????? value="com.security.service.ServerPasswordCallbackHandler"/>
???????? </map>
???? </constructor-arg>
</bean>
?
<jaxws:serverid="helloServiceWs" address="/helloService">
?????<jaxws:serviceBean>
???????? <refbean="helloService" />
???? </jaxws:serviceBean><!--使用这种方法发布web服务 -->
???? <jaxws:inInterceptors>
???????? <refbean="wss4jInInterceptor" />
???? </jaxws:inInterceptors><!—wss4j配置?-->
???? <!--<jaxws:serviceFactory>
???????? <refbean="jaxWsServiceFactoryBean" />
???? </jaxws:serviceFactory>? --><!—数据绑定方式配置?-->
</jaxws:server>
<!--?通过Spring创建数据绑定的类-->
?? <!--<bean id="aegisBean"/>
???<bean id="jaxWsServiceFactoryBean"
value="true" />
???????<property name="dataBinding" ref="aegisBean"/>
???</bean> -->
</beans>
4)配置web.xml
<context-param>
???? <param-name>contextConfigLocation</param-name>
???? <param-value>/WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
?
<!—在tomcat中发布需要配置servlet -->
<servlet>
???? <servlet-name>CXFServlet</servlet-name>
???? <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
???? <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
???? <servlet-name>CXFServlet</servlet-name>
???? <url-pattern>/ws/*</url-pattern>
</servlet-mapping>
5)发布web项目
因为在web.xml里面配置了servlet,则可以将项目发布到tomcat下,启动tomcat即可。
6)访问wsdl
http://localhost:8080/WSCXF/ws/helloService?wsdl
1)新建一个web客户端项目,用wsdl2java生成客户端代码。将生成的客户端代码拷贝到src下。添加spring相关包。
2)配置spring配置文件。
beans.xml存放在src目录下,具体配置如下:
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd">
?
<!-- wss4j?配置在客户端,后面有讲到相关知识 -->
<!--wss4j?客户端配置 -->
<beanid="wss4jOutInterceptor"
value="UsernameToken" />
???????????? <entrykey="user" value="Fetion" />
???????????? <!--<entry key="passwordType" value="PasswordText" />-->
???????????? <!--密码使用MD5密文发送 -->
???????????? <entrykey="passwordDigest" value="PasswordText" />
???????????? <entrykey="passwordCallbackClass"
???????????????? value="com.security.client.ClientPasswordCallbackHandler"/>
???????? </map>
???? </constructor-arg>
</bean>
<jaxws:client id="helloServeiceClient"
???? address="http://localhost:8080/WSCXF/ws/helloService"service/>
??????? </jaxws:outInterceptors><!--wss4j客户端配置-->
</jaxws:client>
</beans>
?
2)调用web服务
在main方法中运行:
ClassPathXmlApplicationContext app = newClassPathXmlApplicationContext("beans.xml");//此处beans.xml放在src下,也需要放在其他目录下,但需要注明清楚
//获取webservice服务的操作接口
IHelloServicehelloService = (IHelloService)app.getBean("helloServeiceClient");
?