搭建工程spring+cxf工程。添加相应的.jar
Interface:
import javax.jws.WebService;
@WebService
public interface UserService {
public String getUserName(String name);
public String getPassWord(String password);
}
Impl:
import javax.jws.WebService;
@WebService(endpointInterface = "com.bsf.gwtservice.UserService")
public class UserServiceImpl implements UserService {
@Override
public String getPassWord(String password) {
return MD5.getMDFivePassword(password);
}
@Override
public String getUserName(String name) {
return name;
}
}
spring-cxf.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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="userServiceImplCxf"
class="com.bsf.gwtservice.UserServiceImpl" />
<jaxws:endpoint id="userService" implementor="#userServiceImplCxf"
address="/userServiceCxf" />
</beans>
注意:<jaxws:endpoint id="userService" implementor="#userServiceImplCxf"
address="/userServiceCxf" />
id:指在spring配置的bean的ID.
Implementor:#+实现类的bean id.
implementorClass="类全路径"
Address:指明这个web service的相对地址
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:xml/*.xml</param-value>
</context-param>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
部署到tomcat服务器,输入:http://localhost:9100/GwtService/userServiceCxf?wsdl
客户端:
用DOS将目录切换到cxf/bin运行wsdl2java.bat生成客户端代码
配置client-spring.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:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:customer="http://customerservice.example.com/"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
">
<jaxws:client id="userService"
address="http://localhost:9100/GwtService/userServiceCxf"
serviceClass="com.bsf.gwtservice.UserService">
</jaxws:client>
</beans>