WebService學習筆記-使用CXF編寫基於Spring的Webservice

新建Web工程,目錄結構如圖
java

wKiom1Q8gu_Ro01BAAENwYpsGGo422.jpg

Order.java實體類web

public class Order {
	private int id;
	private String name;
	private double price;
	///...省略getter setter toString方法
	public Order() {//無參構造函數
		super();
	}
}


OrderWS.java接口spring

package com.umgsai.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

import com.umgsai.beans.Order;
@WebService
public interface OrderWS {

	@WebMethod
	public Order getOrderById(int id);
}


OrderWSImpl.java實現接口apache

package com.umgsai.ws.impl;

import com.umgsai.beans.Order;
import com.umgsai.ws.OrderWS;

public class OrderWSImpl implements OrderWS{

	@Override
	public Order getOrderById(int id) {
		System.out.println("Server getOrderById:" + id);
		return new Order(id, "Car", 200000);
	}

}


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">
   <!-- 引入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" /> 
   <jaxws:endpoint 
     id="orderWS" 
     implementor="com.umgsai.ws.impl.OrderWSImpl" 
     address="/orderWS" />
</beans>


web.xmlapp

<?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>WS_spring</display-name>
  <welcome-file-list>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置beans -->
  <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>
   <!-- 
   	全部的請求都會先通過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>
</web-app>


啓動項目框架

http://localhost:8080/WS_spring/orderWS?wsdl 能夠查看發佈的服務jsp

源碼 http://yunpan.cn/cg4kLXaK8k5HS (提取碼:7c34)ide



客戶端調用上面發佈的服務。函數

wKiom1Q8jM2xayRIAADdoMf5wLo450.jpg

使用CXF自帶的工具生成客戶端代碼

apache-cxf-3.0.1\bin>wsdl2java  http://localhost:8080/WS_spring/orderWS?wsdl


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">
	<jaxws:client id="orderClient" serviceClass="com.umgsai.ws.OrderWS"
		address="http://localhost:8080/WS_spring/orderWS" />
</beans>


client.java 客戶端代碼

package com.umgsai.ws.client;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.umgsai.ws.Order;
import com.umgsai.ws.OrderWS;

public class Client {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("client-beans.xml");
		OrderWS orderWS = (OrderWS) applicationContext.getBean("orderClient");
		Order order = orderWS.getOrderById(23);
		System.out.println(order);
	}
}


客戶端打印結果

Order [id=23, name=Car, price=200000.0]


服務器端打印結果

Server getOrderById:23

相關文章
相關標籤/搜索