首先我用的是spache-cxf-3.2.3版本的。
我是根據spacheCXFWebServiceDevelopment書中寫例子的。
免費資源http://down.51cto.com/data/656589 點擊打開鏈接
服務端的建立,新建動態web項目,目錄如下。
Order類:
package com.gzc.ws_server_cxf_spring.bean;
public class Order {
private String id;
private String name;
private Double price;
public Order() {
super();
}
public Order(String id, String name, Double price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
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 Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Order [id=" + id + ", name=" + name + ", price=" + price + "]";
}
}
接口OrderService:
package com.gzc.ws_server_cxf_spring.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
import com.gzc.ws_server_cxf_spring.bean.Order;
@WebService
public interface OrderService {
@WebMethod
Order getOrderById(String id);
}
實現類OrderServiceImpl:
package com.gzc.ws_server_cxf_spring.service.impl;
import javax.jws.WebService;
import com.gzc.ws_server_cxf_spring.bean.Order;
import com.gzc.ws_server_cxf_spring.service.OrderService;
@WebService
public class OrderServiceImpl implements OrderService{
public OrderServiceImpl(){
System.out.println("OrderServiceImpl 實例化");
}
@Override
public Order getOrderById(String id) {
System.out.println("服務端執行方法");
return new Order("1","美女",6666.0);
}
}
beans.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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 上面寫的書中有下面兩個配置 此版本不用加載下面兩個xml -->
<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="order"
implementor="com.gzc.ws_server_cxf_spring.service.impl.OrderServiceImpl"
address="/Order" />
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-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>
客戶端:新建web工程。
目錄如下:
其中前兩個包的內容是自動生成的,根據CXF命令 wsdl2java(需要配置環境變量)。
TestClient類:
package com.gzc.ws_server_cxf_spring.service.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.gzc.ws_server_cxf_spring.service.Order;
import com.gzc.ws_server_cxf_spring.service.OrderService;
public class TestClient {
public static void main(String[] args) {
ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext(new String[]
{"client_beans.xml"});
OrderService bean = (OrderService)context.getBean("orderClient");//與配置文件中的id保持一致
Order order = bean.getOrderById("1");
System.out.println(order);
}
}
client_beans.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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!--serviceClass爲接口,address爲訪問的地址-->
<jaxws:client id="orderClient" serviceClass="com.gzc.ws_server_cxf_spring.service.OrderService"
address="http://localhost:8088/ws_server_cxf_spring/Order" />
</beans>
最後先運行服務端(sun as server),運行客戶端(run as javaApplication),本人測試成功。。