在applicationContext-ws.xml配置
<import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <!-- jax-ws endpoint定义 --> <jaxws:endpoint address="/userservice"> <jaxws:implementor ref="userWebService" /> </jaxws:endpoint>
定义接口
@WebService(name = "UserService", targetNamespace = WsConstants.NS) public interface { /** * 获取所有用户. */ public GetAllUserResult getAllUser(); }
定义实现
@WebService(serviceName = "UserService", portName = "UserServicePort", endpointInterface = "UserWebService", targetNamespace = WsConstants.NS) public class UserWebServiceImpl implements UserWebService { public GetAllUserResult getAllUser() { .......... } }
web.xml里配置
<servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping>
完成。
客户端:
首先根据wsdl2java编译wsdl文件生成的接口代码。
然后在applicationContext-client.xml里
<bean id="client" class="UserWebService" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="UserWebService"/> <property name="address" value=".../ws/userservice"/> </bean>
获得client就可以调用。
或者直接用:
<jaxws:client id="userWebService" serviceClass="UserWebService" address=".../ws/userservice" />