用cxf编写基于spring的webservice之上篇

用CXF编写基于spring的web service的步骤:

 

1.   Server端

–     创建spring的配置文件beans.xml,在其中配置SEI

–     在web.xml中,配置上CXF的一些核心组件

1.   Client端

–     生成客户端代码

–     创建客户端的spring配置文件beans-client.xml,并配置

–     编写测试类请求web service

下面通过编码实现:

服务端实体类:

 

package com.cxf.entity;

/**
 * @author yxs
 * @since 2015年9月16日 上午10:17:00
 */
public class Order {
	private int id;
	private String name;
	private double price;

	public Order(int id, String name, double price) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
	}

	public Order() {
		super();
	}

	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;
	}

	@Override
	public String toString() {
		return "Order [id=" + id + ", name=" + name + ", price=" + price + "]";
	}
}

接口和实现类:

package com.cxf.ws;

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

import com.cxf.entity.Order;

/**
 * @author yxs
 * @since 2015年9月16日 上午10:17:54
 */
@WebService
public interface OrderWS {

	@WebMethod
	public Order getOrderById(int id);
}

package com.cxf.ws;

import javax.jws.WebService;

import com.cxf.entity.Order;

/**
 * @author yxs
 * @since 2015年9月16日 上午10:18:57
 */
@WebService
public class OrderWSImpl implements OrderWS {

	/* (non-Javadoc)
	 * @see com.cxf.ws.OrderWS#getOrderById(int)
	 */
	@Override
	public Order getOrderById(int id) {
		// TODO Auto-generated method stub
		return new Order(id, "机票",1000);
	}

}

配置文件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/jaxws">
 
 
  <!-- 引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.cxf.ws.OrderWSImpl" address="/orderws">
      <!-- 加一个拦截器方便后面用jquery请求webservice的测试 -->
    	<jaxws:inInterceptors>
     		<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> 
      	</jaxws:inInterceptors> 
    </jaxws:endpoint>
</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" 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">
  <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>
  
  <!-- 配置beans.xml -->
  <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>/ws/*</url-pattern>
    </servlet-mapping>
</web-app>

启动tomcat服务器,然后访问http://localhost:8088/cxf_spring_ws/ws/orderws?wsdl

就可以看到

<wsdl:definitions name="OrderWSImplService"
	targetNamespace="http://ws.cxf.com/">
	<wsdl:types>
		<xs:schema elementFormDefault="unqualified" targetNamespace="http://ws.cxf.com/"
			version="1.0">
			<xs:element name="getOrderById" type="tns:getOrderById" />
			<xs:element name="getOrderByIdResponse" type="tns:getOrderByIdResponse" />
			<xs:complexType name="getOrderById">
				<xs:sequence>
					<xs:element name="arg0" type="xs:int" />
				</xs:sequence>
			</xs:complexType>
			<xs:complexType name="getOrderByIdResponse">
				<xs:sequence>
					<xs:element minOccurs="0" name="return" type="tns:order" />
				</xs:sequence>
			</xs:complexType>
			<xs:complexType name="order">
				<xs:sequence>
					<xs:element name="id" type="xs:int" />
					<xs:element minOccurs="0" name="name" type="xs:string" />
					<xs:element name="price" type="xs:double" />
				</xs:sequence>
			</xs:complexType>
		</xs:schema>
	</wsdl:types>
	<wsdl:message name="getOrderByIdResponse">
		<wsdl:part element="tns:getOrderByIdResponse" name="parameters">
		</wsdl:part>
	</wsdl:message>
	<wsdl:message name="getOrderById">
		<wsdl:part element="tns:getOrderById" name="parameters">
		</wsdl:part>
	</wsdl:message>
	<wsdl:portType name="OrderWS">
		<wsdl:operation name="getOrderById">
			<wsdl:input message="tns:getOrderById" name="getOrderById">
			</wsdl:input>
			<wsdl:output message="tns:getOrderByIdResponse" name="getOrderByIdResponse">
			</wsdl:output>
		</wsdl:operation>
	</wsdl:portType>
	<wsdl:binding name="OrderWSImplServiceSoapBinding" type="tns:OrderWS">
		<soap:binding style="document"
			transport="http://schemas.xmlsoap.org/soap/http" />
		<wsdl:operation name="getOrderById">
			<soap:operation soapAction="" style="document" />
			<wsdl:input name="getOrderById">
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output name="getOrderByIdResponse">
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="OrderWSImplService">
		<wsdl:port binding="tns:OrderWSImplServiceSoapBinding" name="OrderWSImplPort">
			<soap:address location="http://localhost:8088/cxf_spring_ws/ws/orderws" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>

说明发布成功.


新建一个web项目,将服务端的jar都拷过来,通过wsdl2java生成客户端代码

配置文件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/jaxws">
	
	<jaxws:client id="orderClient" serviceClass="com.cxf.ws.OrderWS" address="http://localhost:8088/cxf_spring_ws/ws/orderws">
	     <jaxws:outInterceptors>
	            <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
	     </jaxws:outInterceptors>
	</jaxws:client>
	
	
	</beans>

写一个测试类就可以看到输出的信息,表示客户端调用成功:

package com.cxf.ws;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author yxs
 * @since 2015年9月16日 上午10:28:59
 */
public class TestCxf {
     public static void main(String[] args) {
		   ApplicationContext applicationContext=new ClassPathXmlApplicationContext(new String[]  {"client-beans.xml"});
    	   OrderWS bean = (OrderWS) applicationContext.getBean("orderClient");
    	   Order order=bean.getOrderById(22);
    	   System.out.println(order);
	}
}

下一篇,我们用页面的js调用一个servlet,然后通过后台访问webservice,因为直接使用js访问webservice会存在跨域问题


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值