1、下载包http://cxf.apache.org/download.html
2、在Eclipse中创建一个web项目,把上面解压后的jar包全部拷贝到WEB-INF/lib下。
3、JavaBean类
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String pwd;
public String getName() {
return name;
}
public String getPwd() {
return pwd;
}
public void setName(String name) {
this.name = name;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
4、UserService接口
@WebService
public interface UserService {
public User getUser(@WebParam(name="name")String name, @WebParam(name="pwd")String pwd);
public List<User> getUsers();
}
5、UserService实现类
@WebService(endpointInterface="cn.com.cncloud.UserService", serviceName="userServiceImpl")
public class UserServiceImpl implements UserService {
@Override
public User getUser(String name, String pwd) {
User user = new User();
user.setName(name);
user.setPwd(pwd);
return user;
}
@Override
public List<User> getUsers() {
User u1 = new User();
u1.setName("HanMeiMei");
u1.setPwd("123456");
User u2 = new User();
u2.setName("LiLei");
u2.setPwd("123456");
List<User> list = new ArrayList<User>();
list.add(u1);
list.add(u2);
return list;
}
}
6、测试类
public class ServerTest {
public static void main(String[] args) {
UserService userService = new UserServiceImpl();
Endpoint.publish("http://localhost:8080/userServiceImpl", userService);
}
}
7、由于CFX自带了一个轻量级的Spring容器提供IOC服务,我们可以先启动测试部署是否成功,直接运行Test类
8、在浏览器中输入http://localhost:8080/userServiceImpl?wsdl,浏览器显示以下内容:
\u2212
<wsdl:definitions name="userServiceImpl" targetNamespace="http://cncloud.com.cn/">
\u2212
<wsdl:types>
\u2212
<xs:schema elementFormDefault="unqualified" targetNamespace="http://cncloud.com.cn/" version="1.0">
<xs:element name="getUser" type="tns:getUser"/>
<xs:element name="getUserResponse" type="tns:getUserResponse"/>
<xs:element name="getUsers" type="tns:getUsers"/>
<xs:element name="getUsersResponse" type="tns:getUsersResponse"/>
\u2212
<xs:complexType name="getUser">
\u2212
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string"/>
<xs:element minOccurs="0" name="pwd" type="xs:string"/>
</xs:sequence>
</xs:complexType>
\u2212
<xs:complexType name="getUserResponse">
\u2212
<xs:sequence>
<xs:element minOccurs="0" name="return" type="tns:user"/>
</xs:sequence>
</xs:complexType>
\u2212
<xs:complexType name="user">
\u2212
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string"/>
<xs:element minOccurs="0" name="pwd" type="xs:string"/>
</xs:sequence>
</xs:complexType>
\u2212
<xs:complexType name="getUsers">
<xs:sequence/>
</xs:complexType>
\u2212
<xs:complexType name="getUsersResponse">
\u2212
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="return" type="tns:user"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
\u2212
<wsdl:message name="getUsers">
<wsdl:part element="tns:getUsers" name="parameters">
</wsdl:part>
</wsdl:message>
\u2212
<wsdl:message name="getUserResponse">
<wsdl:part element="tns:getUserResponse" name="parameters">
</wsdl:part>
</wsdl:message>
\u2212
<wsdl:message name="getUser">
<wsdl:part element="tns:getUser" name="parameters">
</wsdl:part>
</wsdl:message>
\u2212
<wsdl:message name="getUsersResponse">
<wsdl:part element="tns:getUsersResponse" name="parameters">
</wsdl:part>
</wsdl:message>
\u2212
<wsdl:portType name="UserService">
\u2212
<wsdl:operation name="getUser">
<wsdl:input message="tns:getUser" name="getUser">
</wsdl:input>
<wsdl:output message="tns:getUserResponse" name="getUserResponse">
</wsdl:output>
</wsdl:operation>
\u2212
<wsdl:operation name="getUsers">
<wsdl:input message="tns:getUsers" name="getUsers">
</wsdl:input>
<wsdl:output message="tns:getUsersResponse" name="getUsersResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
\u2212
<wsdl:binding name="userServiceImplSoapBinding" type="tns:UserService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
\u2212
<wsdl:operation name="getUser">
<soap:operation soapAction="" style="document"/>
\u2212
<wsdl:input name="getUser">
<soap:body use="literal"/>
</wsdl:input>
\u2212
<wsdl:output name="getUserResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
\u2212
<wsdl:operation name="getUsers">
<soap:operation soapAction="" style="document"/>
\u2212
<wsdl:input name="getUsers">
<soap:body use="literal"/>
</wsdl:input>
\u2212
<wsdl:output name="getUsersResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
\u2212
<wsdl:service name="userServiceImpl">
\u2212
<wsdl:port binding="tns:userServiceImplSoapBinding" name="UserServiceImplPort">
<soap:address location="http://localhost:8080/userServiceImpl"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
9、客户端访问服务
public class ClientTest {
public static void main(String[] args) {
JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
svr.setServiceClass(UserService.class);
svr.setAddress("http://localhost:8080/userServiceImpl");
UserService us = (UserService)svr.create();
User user = us.getUser("Jack", "123");
System.out.println(user.getName());
}
}
集成Spring
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String pwd;
public String getName() {
return name;
}
public String getPwd() {
return pwd;
}
public void setName(String name) {
this.name = name;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
@WebService
public interface UserService {
public User getUser(@WebParam(name="name")String name, @WebParam(name="pwd")String pwd);
public List<User> getUsers();
}
public class UserServiceImpl implements UserService {
@Override
public User getUser(String name, String pwd) {
User user = new User();
user.setName(name);
user.setPwd(pwd);
return user;
}
@Override
public List<User> getUsers() {
User u1 = new User();
u1.setName("HanMeiMei");
u1.setPwd("123456");
User u2 = new User();
u2.setName("LiLei");
u2.setPwd("123456");
List<User> list = new ArrayList<User>();
list.add(u1);
list.add(u2);
return list;
}
}
public class ClientSpringTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService)context.getBean("userServiceImplClient");
User user = us.getUser("Jack", "111111");
System.out.println(user.getName());
System.out.println(user.getPwd());
}
}
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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
default-lazy-init="true">
<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"/>
<!-- 服务器端配置 -->
<jaxws:endpoint id="userServiceImpl"
implementor="cn.com.cncloud.UserServiceImpl"
address="/userServiceImpl" />
<!-- 客户端配置 -->
<jaxws:client id="userServiceImplClient"
serviceClass="cn.com.cncloud.UserService"
address="http://localhost:8080/WSSpring/userServiceImpl" />
</beans>
web.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>WSSpring</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
</web-app>