加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

cxf WebService整合Spring

发布时间:2020-12-16 22:42:53 所属栏目:安全 来源:网络整理
导读:第一步创建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 pri

第一步创建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.1bin

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读