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

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010741376/article/details/48517561

用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会存在跨域问题


相关文章
|
9月前
phpstorm插件应用:Test RESTful WEB Service 控制台接口调试工具
phpstorm插件应用:Test RESTful WEB Service 控制台接口调试工具
118 0
|
1月前
|
存储 缓存 算法
关于 Service Worker 和 Web 应用对应关系的讨论
关于 Service Worker 和 Web 应用对应关系的讨论
14 0
|
2月前
|
Java API Apache
Apache CXF生成WebService的客户端
Apache CXF生成WebService的客户端
|
6月前
|
JSON 安全 API
使用 ABAP sproxy 事务码生成的 Proxy 消费 Web Service
使用 ABAP sproxy 事务码生成的 Proxy 消费 Web Service
56 0
|
2月前
|
XML 网络架构 数据格式
Ruby 教程 之 Ruby Web Service 应用 - SOAP4R 2
Ruby Web Service 应用 - SOAP4R
24 5
|
2月前
|
XML Linux 网络架构
Ruby 教程 之 Ruby Web Service 应用 - SOAP4R 1
Ruby Web Service 应用 - SOAP4R
23 3
|
8月前
|
XML Java API
Java Web Service Get请求使用指南
Java Web Service Get请求使用指南 在当今互联网时代,Web Service已经成为了现代软件开发中不可或缺的一部分。而Java作为一种广泛使用的编程语言,自然也提供了丰富的工具和库来支持Web Service的开发。本文将为大家介绍如何使用Java编程语言进行Web Service的Get请求。
87 0
|
4月前
|
Java 数据库连接 Apache
SpringBoot整合CXF实现WebService
SpringBoot整合CXF实现WebService
126 0
|
4月前
|
XML Java 应用服务中间件
WebService - Axis2与Spring整合并发布多个service(同样使用services.xml)
WebService - Axis2与Spring整合并发布多个service(同样使用services.xml)
81 0
|
4月前
|
Java 应用服务中间件 Spring
WebService - Axis2使用services.xml进行开发server与client(未与Spring整合)
WebService - Axis2使用services.xml进行开发server与client(未与Spring整合)
44 0