微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cxf WebService整合Spring

第一步创建web项目

可以把apache-cxf-3.1.1中lib可以全部拷贝到项目中lib文件

http://download.csdn.net/detail/duankelin/8949701 cxf下载

编写WebService 服务端

1.实体类

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

2.WebService 接口方法  (最终暴露在外交由别人使用的方法)

package com.test.ws;

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

import com.test.bean.Order;
@WebService
public interface OrderWs {
	@WebMethod
	public Order getorderById(int id);
}

3.接口实现

package com.test.ws;

import javax.jws.WebService;

import com.test.bean.Order;
@WebService
public class OrderWsImpl implements OrderWs {
	public OrderWsImpl(){
		System.out.println("OrderWsImpl();");
	}
	
	@Override
	public Order getorderById(int id) {
		System.out.println("server getById");
		return new Order(id,"飞机",100000);
	}

}

4.编写websevices 发布文件bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws.xsd">
    
    
    <import resource="classpath:meta-inf/cxf/cxf.xml"/>  这三个文件是cxf的 
    <import resource="classpath:meta-inf/cxf/cxf-extension-soap.xml"/>
    <import resource="classpath:meta-inf/cxf/cxf-servlet.xml"/>
    
    <jaxws:endpoint id="orderWs"  id随意
     implementor="com.test.ws.OrderWsImpl" <span style="color:#FF0000;">接口实现</span>
      address="/orderws"    />  <span style="color:#FF0000;">WebService 发布路径</span>
</beans>

5.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">
  <display-name>cxf_Spring</display-name>
  <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>
  
  <!-- 加载spring容器配置 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 设置spring容器加载配置文件路径 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:bean.xml</param-value>
</context-param>

<servlet>
    <servlet-name>CXFService</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>CXFService</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

6.启动项目 自动完成webservices发布,显示结果如下:

请求地址:http://127.0.0.1:8089/cxf_Spring/orderws?wsdl    与bean.xm中的address对应

<?xml version="1.0" encoding="UTF-8"?>

-<wsdl:deFinitions targetNamespace="http://ws.test.com/" name="OrderWsImplService" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.test.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">


-<wsdl:types>


-<xs:schema targetNamespace="http://ws.test.com/" xmlns:tns="http://ws.test.com/" version="1.0" elementFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<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 name="return" type="tns:order" minOccurs="0"/>

</xs:sequence>

</xs:complexType>


-<xs:complexType name="order">


-<xs:sequence>

<xs:element name="id" type="xs:int"/>

<xs:element name="name" type="xs:string" minOccurs="0"/>

<xs:element name="price" type="xs:double"/>

</xs:sequence>

</xs:complexType>

</xs:schema>

</wsdl:types>


-<wsdl:message name="getorderByIdResponse">

<wsdl:part name="parameters" element="tns:getorderByIdResponse"> </wsdl:part>

</wsdl:message>


-<wsdl:message name="getorderById">

<wsdl:part name="parameters" element="tns:getorderById"> </wsdl:part>

</wsdl:message>


-<wsdl:portType name="OrderWs">


-<wsdl:operation name="getorderById">

<wsdl:input name="getorderById" message="tns:getorderById"> </wsdl:input>

<wsdl:output name="getorderByIdResponse" message="tns:getorderByIdResponse"> </wsdl:output>

</wsdl:operation>

</wsdl:portType>


-<wsdl:binding name="OrderWsImplServiceSoapBinding" type="tns:OrderWs">

<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>


-<wsdl:operation name="getorderById">

<soap:operation style="document" soapAction=""/>


-<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 name="OrderWsImplPort" binding="tns:OrderWsImplServiceSoapBinding">

<soap:address location="http://127.0.0.1:8089/cxf_Spring/orderws"/>

</wsdl:port>

</wsdl:service>

</wsdl:deFinitions>


二 客户端测试

1.新建web项目,利用cxf工具生客户端代码方法如下

运行cmd 命令,在dos下进入项目src下

执行命令wsdl2java  wsdl 文件   回车就ok了   刷新项目 就能看到生成代码

如果出现不是内部命令的提示 则需要在系统变量PATH中配置cxf 如:D:\apache-cxf-3.1.1\bin

2.编写客户端client-bean.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws.xsd">
    
    
    <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:client id="<span style="color:#FF0000;">orderClient</span>" serviceClass="<span style="color:#FF0000;">com.test.ws.OrderWs</span>"  <span style="color:#FF0000;">接口全路径</span>
    address="<span style="color:#FF0000;">http://127.0.0.1:8089/cxf_Spring/orderws</span>"/> 接口地址
</beans>
3.编写测试类

package com.test.servserTest;



import org.springframework.context.support.ClasspathXmlApplicationContext;

import com.test.ws.Order;
import com.test.ws.OrderWs;
import com.test.ws.OrderWsImplService;

public class Test {
	public static void main(String[] args) {
		/*ClasspathXmlApplicationContext context  = new ClasspathXmlApplicationContext(new String[]{"client-bean.xml"}); 
		OrderWs b =(OrderWs) context.getBean("orderClient");
		Order order = b.getorderById(25);
		System.out.println(order);*/
		
		OrderWsImplService implService = new OrderWsImplService();
		OrderWs orderWs = implService.getorderWsImplPort();
		
		Order a = orderWs.getorderById(86);
		System.out.println(a);
	}
}

两种方法都可以,最终结果如下:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐