WebService学习笔记-使用CXF编写基于Spring的Webservice

来源:互联网 发布:淘宝是猫京东是狗 编辑:程序博客网 时间:2024/05/17 04:52

新建Web工程,目录结构如图

wKiom1Q8gu_Ro01BAAENwYpsGGo422.jpg

Order.java实体类

public class Order {private int id;private String name;private double price;///...省略getter setter toString方法public Order() {//无参构造函数super();}}


OrderWS.java接口

package com.umgsai.ws;import javax.jws.WebMethod;import javax.jws.WebService;import com.umgsai.beans.Order;@WebServicepublic interface OrderWS {@WebMethodpublic Order getOrderById(int id);}


OrderWSImpl.java实现接口

package com.umgsai.ws.impl;import com.umgsai.beans.Order;import com.umgsai.ws.OrderWS;public class OrderWSImpl implements OrderWS{@Overridepublic Order getOrderById(int id) {System.out.println("Server getOrderById:" + id);return new Order(id, "Car", 200000);}}


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/schemas/jaxws.xsd">   <!-- 引入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.umgsai.ws.impl.OrderWSImpl"      address="/orderWS" /></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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>WS_spring</display-name>  <welcome-file-list>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>    <!-- 配置beans -->  <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>/*</url-pattern>   </servlet-mapping></web-app>


启动项目

http://localhost:8080/WS_spring/orderWS?wsdl 可以查看发布的服务

源码 http://yunpan.cn/cg4kLXaK8k5HS (提取码:7c34)



客户端调用上面发布的服务。

wKiom1Q8jM2xayRIAADdoMf5wLo450.jpg

使用CXF自带的工具生成客户端代码

apache-cxf-3.0.1\bin>wsdl2java  http://localhost:8080/WS_spring/orderWS?wsdl


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.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><jaxws:client id="orderClient" serviceClass="com.umgsai.ws.OrderWS"address="http://localhost:8080/WS_spring/orderWS" /></beans>


client.java 客户端代码

package com.umgsai.ws.client;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.umgsai.ws.Order;import com.umgsai.ws.OrderWS;public class Client {public static void main(String[] args) {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("client-beans.xml");OrderWS orderWS = (OrderWS) applicationContext.getBean("orderClient");Order order = orderWS.getOrderById(23);System.out.println(order);}}


客户端打印结果

Order [id=23, name=Car, price=200000.0]


服务器端打印结果

Server getOrderById:23

本文出自 “优赛工作室” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1563774

0 0