項目代碼層次結構:java
①:web.xml:CXFServlet/Spring配置文件信息web
context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <!-- web容器啓動時加載配置的xml文件 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 配置CXF框架 --> <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>
②:spring整合CXF框架配置:beans.xmlspring
<!-- 引cxf的一些核心配置 --> <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" /> <!-- 配置webservice的終端接口: id:惟一的識別碼 implementor:webservice接口的實現類 address:web訪問的地址:http://localhost:8080/webservice_spring_server/orderws?wsdl --> <jaxws:endpoint id="orderWS" implementor="com.zhiwei.ws.service.OrderWSImp" address="/orderws"> <!-- 配置服務器的出攔截器 --> <jaxws:inInterceptors> <bean class="com.zhiwei.ws.interceptor.ServerInterceptor"/> </jaxws:inInterceptors>
③:webservice服務接口/服務類/攔截器/POJOapache
package com.zhiwei.ws.inter; import javax.jws.WebMethod; import javax.jws.WebService; import com.zhiwei.domain.Order; @WebService public interface OrderWS { @WebMethod public Order getOrderById(int id); }
package com.zhiwei.ws.service; import javax.jws.WebService; import com.zhiwei.domain.Order; import com.zhiwei.ws.inter.OrderWS; @WebService public class OrderWSImp implements OrderWS { //SEI實現類由spring容器管理,自動實例化加載 public OrderWSImp() { System.out.println("webserviceIMP被建立......."); } public Order getOrderById(int id) { System.out.println("getOrderById is running!"); return new Order(id, "squirrel", 200.5); } }
package com.zhiwei.ws.interceptor; import java.util.List; import org.apache.cxf.binding.soap.SoapHeader; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.headers.Header; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.w3c.dom.Element; public class ServerInterceptor extends AbstractPhaseInterceptor<SoapMessage> { public ServerInterceptor() { super(Phase.PRE_PROTOCOL); } //<head> <name>zhiwei<name> </head> public void handleMessage(SoapMessage msg) throws Fault { //獲取文件頭 List<Header> headers=msg.getHeaders(); SoapHeader header=(SoapHeader) headers.get(0); Element element=(Element) header.getObject(); String name=element.getElementsByTagName("name").item(0).getTextContent(); if("xiaoyang".equals(name)) { System.out.println("Server interceptor.........."+name); return; } throw new Fault(new RuntimeException("用戶名不正確!")); } }
package com.zhiwei.domain; public class Order { private int id; private String name; private double price; public int getId() { return id; } public void setId(int 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; } public Order(int id, String name, double price) { super(); this.id = id; this.name = name; this.price = price; } @Override public String toString() { return "Order [id=" + id + ", name=" + name + ", price=" + price + "]"; } }
客戶端代碼層次結構:客戶端代碼採用CXF的wsdl2java工具生成服務器
④:客戶端spring配置:client-beans.xmlapp
<jaxws:client id="orderClient" serviceClass="com.zhiwei.ws.service.OrderWS" address="http://localhost:8080/webservice_spring_server/orderws"> <!-- 配置客戶端的出攔截器 --> <jaxws:outInterceptors> <bean class="com.zhiwei.ws.interceptor.ClientInterceptor"> < constructor-arg name="name" value="xiaoyang"/> </bean> </jaxws:outInterceptors> </jaxws:client>
⑤:客戶端測試框架
//獲取客戶端配置文件 ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("client-beans.xml"); //獲取spring容器裏面的bean OrderWS orderWS=(OrderWS) context.getBean("orderClient"); //經過webservice代理調用webservice方法 Order order=orderWS.getOrderById(1); System.out.println("--->"+order.toString());
客戶端日誌:dom
服務端日誌:ide
至此Spring整合CXF框架完成.....................工具