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

基于cxf webservice(1)

发布时间:2020-12-16 22:42:30 所属栏目:安全 来源:网络整理
导读:由于cxf的框架能集成到Spring中,所以在实际项目中开发webservice多采取此种模式的,接下来的时间基于印度人写的ApacheCXFWebServiceDevelopment这本书留下痕迹,用以证明自己曾经学习过,努力过,也无奈过……。 一、webservice服务端的建立 首先建立一个we

由于cxf的框架能集成到Spring中,所以在实际项目中开发webservice多采取此种模式的,接下来的时间基于印度人写的ApacheCXFWebServiceDevelopment这本书留下痕迹,用以证明自己曾经学习过,努力过,也无奈过……。

一、webservice服务端的建立

首先建立一个web工程目录结构如下:


Order.java

package demo.order;

import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name="Order")
public class Order {

		private String customerId;
		private String itemId;
		private int qty;
		private double price;
		public String getCustomerId() {
			return customerId;
		}
		public void setCustomerId(String customerId) {
			this.customerId = customerId;
		}
		public String getItemId() {
			return itemId;
		}
		public void setItemId(String itemId) {
			this.itemId = itemId;
		}
		public int getQty() {
			return qty;
		}
		public void setQty(int qty) {
			this.qty = qty;
		}
		public double getPrice() {
			return price;
		}
		public void setPrice(double price) {
			this.price = price;
		}
		public Order(String customerId,String itemId,int qty,double price) {
			super();
			this.customerId = customerId;
			this.itemId = itemId;
			this.qty = qty;
			this.price = price;
		}
		public Order() {
			super();
		}
		@Override
		public String toString() {
			return "Order [customerId=" + customerId + ",itemId=" + itemId
					+ ",qty=" + qty + ",price=" + price + "]";
		}
		
}

OrderProcess.java

package demo.order;

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

@WebService
public interface OrderProcess {
	
	@WebMethod
	String processOrder(Order order);
	
}

OrderProcessImpl.java

package demo.order;

import javax.jws.WebService;

@WebService
public class OrderProcessImpl implements OrderProcess {

	public OrderProcessImpl(){
		//提示发布成功
		System.out.println("..........AA...AA.........");
	}
	
	private String validate(Order order) {
		String orderId = validate(order);
		System.out.println("发布成功!");
		return orderId;
	}

	@Override
	public String processOrder(Order order) {

		String custId = order.getCustomerId();
		String itemId = order.getItemId();
		int qty = order.getQty();
		double price = order.getPrice();
		if (custId != null && itemId != null && !custId.equals("")
				&& !itemId.equals("") && qty > 0 && price > 0.0) {
			return "ORD1234";
		}

		return null;
	}

}

最后就是配置文件

bean.xml

注意:新版的和书上有差别,查看官方配置文件:http://cxf.apache.org/docs/writing-a-service-with-spring.html

<?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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
		
		
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	
	<jaxws:endpoint id="orderProcess" implementor="demo.order.OrderProcessImpl"
		address="/orderprocess" />



</beans>

最后就是web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>webService2</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>
  
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>beans.xml</param-value>
  </context-param>
  
  <listener>
  	<listener-class>
  		org.springframework.web.context.ContextLoaderListener
  	</listener-class>
  </listener>
  
  <servlet>
  	<servlet-name>CXFServlet</servlet-name>
  	<display-name>CXF Servlet</display-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>

由于我们建立的是web工程,所以不用写测试类去调用SEI的publish函数去发布了,直接是把我们这个工程发布到server上就可以了。

在浏览器上我们输入:http://localhost:8080/webService2/orderprocess?wsdl

可以看到如下文件内容:

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://order.demo/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="OrderProcessImplService" targetNamespace="http://order.demo/">
  <wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://order.demo/" elementFormDefault="unqualified" targetNamespace="http://order.demo/" version="1.0">

  <xs:element name="Order" type="tns:order"/>

  <xs:element name="processOrder" type="tns:processOrder"/>

  <xs:element name="processOrderResponse" type="tns:processOrderResponse"/>

  <xs:complexType name="processOrder">
    <xs:sequence>
      <xs:element minOccurs="0" name="arg0" type="tns:order"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="order">
    <xs:sequence>
      <xs:element minOccurs="0" name="customerId" type="xs:string"/>
      <xs:element minOccurs="0" name="itemId" type="xs:string"/>
      <xs:element name="price" type="xs:double"/>
      <xs:element name="qty" type="xs:int"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="processOrderResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="return" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>
  </wsdl:types>
  <wsdl:message name="processOrderResponse">
    <wsdl:part element="tns:processOrderResponse" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="processOrder">
    <wsdl:part element="tns:processOrder" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="OrderProcess">
    <wsdl:operation name="processOrder">
      <wsdl:input message="tns:processOrder" name="processOrder">
    </wsdl:input>
      <wsdl:output message="tns:processOrderResponse" name="processOrderResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="OrderProcessImplServiceSoapBinding" type="tns:OrderProcess">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="processOrder">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="processOrder">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="processOrderResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="OrderProcessImplService">
    <wsdl:port binding="tns:OrderProcessImplServiceSoapBinding" name="OrderProcessImplPort">
      <soap:address location="http://localhost:8080/webService2/orderprocess"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>
WSDL文件就是webService交互的核心文件。看到如上wsdl文件,证明我们的服务端启动成功。


二、客户端(本例中我们是把服务器端和客户端放在一个项目中了)

在上述图片中的demo.order.client中建立如图的两个文件

Client.java

package demo.order.client;

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

import demo.order.Order;
import demo.order.OrderProcess;

public final class Client {
	
	public Client() {}

	public static void main(String[] args) {
		
		ApplicationContext act =  new ClassPathXmlApplicationContext(new String[]{"demoorderclientclient-beans.xml"});
		OrderProcess client = (OrderProcess) act.getBean("orderClient");
		
		Order order = new Order();
		order.setCustomerId("C001");
		order.setItemId("I001");
		order.setQty(100);
		order.setPrice(2000.10);
		String orderId = client.processOrder(order);
		String message = (orderId == null)?"order not approved":"order approved;order id is "+orderId;
		System.out.println(message);
		System.exit(0);
		
	}
}

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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
		
		
	
	
	<jaxws:client id="orderClient" serviceClass="demo.order.OrderProcess" address="http://localhost:8080/webService2/orderprocess"/>
	
	
</beans>

运行测试函数main,

得到如下结果:

(编辑:李大同)

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

    推荐文章
      热点阅读