cxf3.1.4整合spring4.1.7 demo-xs
1、开发工具:Eclipse4.7 + tomcat7 + jdk7 + cxf3.1.4 + spring4.1.7
2、首先Eclipse新建两个web工程,,一个模拟server端发布webservice,,一个模拟client端,调用webservice,,如下图,我建的两个工程:分别为springcxfclient1和springcxfservice1
3、将下载的apache-cxf-3.1.4解压后,在lib目录下的所有jar文件(其实不需要所有,这里自己懒得选了,就选所有了),copy到springcxfservice1工程的lib目录 下,因为我这个apache-cxfcopy过来的jar包中已经有了spring的相关jar包,,所以,无需再去spring包下找jar了,
将jar包buildpath
4.服务端写测试代码,一个sei,一个sei实现类,将sei按标准配置下交由spring进行管理,运行tomcat自动发布
测试代码及目录结构如下:
其中import 的cxf.xml是 springcxfservice1-->Libraries-->Web AppLibraries-->cxf-core-3.1.4.jar->META-INF/cxf/cxf.xml
注意要将 Window-->properties-->Catalog-->add 将本地jaxws.xsd加进来(且编译和发布及访问均需联网才有效)如图:
代码如下:
pojo类:
package com.spcxfwebsercie.pojo;
public classUser {
privateString id;
privateString name;
privateString password;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
SEI及实现类:
--SEI:
package com.spcxfwebsercie.service;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.spcxfwebsercie.pojo.User;
@WebService
public interface IWebservice {
@WebMethod
publicString getName();
@WebMethod
publicUser getUserById(@WebParam(name="id") String id);
@WebMethod
publicList<User> getUsers();
}
--SEI实现类:
package com.spcxfwebsercie.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
import com.spcxfwebsercie.pojo.User;
@WebService
public classWebserviceImpl implements IWebservice {
private Map<String,User> map=null;
public WebserviceImpl() {
map=newHashMap<String,User>();
for(int i=1000;i<1021;i++){
Useruser= newUser("hac"+i, "小明"+i, UUID.randomUUID().toString());
map.put("hac"+i, user);
}
}
@Override
public String getName() {
return "helloworld "+"name";
}
@Override
public User getUserById(Stringid) {
Useruser= this.map.get(id);
return user;
}
@Override
public List<User>getUsers() {
List<User>users=newArrayList<User>();
for (User user : map.values()) {
users.add(user);
}
return users;
}
}
5、配置web.xml整合spring,,,,配置如下:
<?xml version="1.0"encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>springcxfserver02</display-name>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean.xml</param-value>
</context-param>
<!-- 加载Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>
6、在src目录下,新建一个spring配置文件bean-service.xml,,,配置如下:
<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- cxf3以后,只需要引入这个配置文件即可,其他两个废弃掉了,必再引-->
<import resource="classpath*:META-INF/cxf/cxf.xml"/>
<jaxws:endpoint id="userService"implementor="com.spcxfwebsercie.service.WebserviceImpl"address="/userWS"/>
</beans>
7、将web工程部署到tomcat中
cxf会根据
web.xml中的cxfservlet和bean-service.xml的javaxws:endpoint 的addresss的值发布wsdl url
如:
web.xml中的代码片断:
bean-service.xml下的代码片断:
服务端发布的wsdl url为:http://localhost:8012/springcxfservice1/webservice/userWS?wsdl
启动tomcat,,,在浏览器中输入发布后的,wsdl地址:
http://localhost:8080/cxfserver/webservice/userWS?wsdl
如下:证明发布成功:
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://service.spcxfwebsercie.com/"xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="WebserviceImplService" targetNamespace="http://service.spcxfwebsercie.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.spcxfwebsercie.com/" elementFormDefault="unqualified" targetNamespace="http://service.spcxfwebsercie.com/"version="1.0">
<xs:element name="getName" type="tns:getName"/>
<xs:element name="getNameResponse" type="tns:getNameResponse"/>
<xs:element name="getUserById" type="tns:getUserById"/>
<xs:element name="getUserByIdResponse" type="tns:getUserByIdResponse"/>
<xs:element name="getUsers" type="tns:getUsers"/>
<xs:element name="getUsersResponse" type="tns:getUsersResponse"/>
<xs:complexType name="getUserById">
<xs:sequence>
<xs:element minOccurs="0" name="id" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getUserByIdResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="tns:user"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="user">
<xs:sequence>
<xs:element minOccurs="0" name="id" type="xs:string"/>
<xs:element minOccurs="0" name="name" type="xs:string"/>
<xs:element minOccurs="0" name="password" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getName">
<xs:sequence/>
</xs:complexType>
<xs:complexType name="getNameResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getUsers">
<xs:sequence/>
</xs:complexType>
<xs:complexType name="getUsersResponse">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="return" type="tns:user"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="getNameResponse">
<wsdl:part element="tns:getNameResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="getName">
<wsdl:part element="tns:getName" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="getUserByIdResponse">
<wsdl:part element="tns:getUserByIdResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="getUsersResponse">
<wsdl:part element="tns:getUsersResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="getUsers">
<wsdl:part element="tns:getUsers" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="getUserById">
<wsdl:part element="tns:getUserById" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="IWebservice">
<wsdl:operation name="getUserById">
<wsdl:input message="tns:getUserById" name="getUserById"></wsdl:input>
<wsdl:output message="tns:getUserByIdResponse" name="getUserByIdResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="getName">
<wsdl:input message="tns:getName" name="getName"></wsdl:input>
<wsdl:output message="tns:getNameResponse" name="getNameResponse"></wsdl:output>
</wsdl:operation>
<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>
<wsdl:binding name="WebserviceImplServiceSoapBinding" type="tns:IWebservice">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getUserById">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getUserById">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getUserByIdResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getName">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getName">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getNameResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getUsers">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getUsers">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getUsersResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="WebserviceImplService">
<wsdl:port binding="tns:WebserviceImplServiceSoapBinding" name="WebserviceImplPort">
<soap:address location="http://localhost:8081/springcxfservice1/webservice/userWS"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
wsdl文档结构图,从下面看到上面
8、服务端写好后,测试服务端发布的是否可以调用
一。根据wsdl生成客户端数据
wsimport -sD:/installSoftware/eclipse/workspace/springcxfclient1/src -pcom.xiangshuai.client
-keephttp://localhost:8081/springcxfservice1/webservice/userWS?wsdl
生成结果如下图:
package com.xiangshuai.test;
import java.util.List;
import com.xiangshuai.client.IWebservice;
import com.xiangshuai.client.User;
import com.xiangshuai.client.WebserviceImplService;
public classWebServiceClientTest {
publicstaticvoidmain(String[] args){
//创建SEI的工厂对象
WebserviceImplServicefactory= new WebserviceImplService();
//得到一个SEI实现类对象
IWebserviceport= factory.getWebserviceImplPort();
//调用SEI的方法,此时才去发送web Service请求,并得到返回结果
List<User>users= port.getUsers();
for (User user : users) {
System.out.println(user.getId()+user.getName()+user.getPassword());
}
}
}