WebService - CXF 与Spring整合(Service+Client)

简介: WebService - CXF 与Spring整合(Service+Client)

【1】编写服务端代码

① 项目结构


② service

package com.web.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
import com.web.bean.Order;
//通过注解@WebService申明为webservice接口 
@WebService
public interface OrderWS {
    //@WebMethod 接口的方法
    //@WebParam给参数命名,提高可代码可读性,此项可选
    @WebMethod
    public Order getOrderById(@WebParam(name="id") int id);
}

③ impl

package com.web.service.impl;
import javax.jws.WebService;
import com.web.bean.Order;
import com.web.service.OrderWS;
@WebService
public class OrderWSImpl implements OrderWS{
    public OrderWSImpl() {
        // TODO Auto-generated constructor stub
        System.out.println("OrderWSImpl is inited ...");
    }
    @Override
    public Order getOrderById(int id) {
        System.out.println("server getOrderById"+id);
        return new Order(id, "tom", 12.02);
    }
}

④ bean-order

package com.web.bean;
public class Order {
    private int id;
    private String name;
    private double 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;
    }
    public Order(int id, String name, double price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }
    public Order() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "Order [id=" + id + ", name=" + name + ", price=" + price + "]";
    }
}

【2】配置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的一些核心配置 3.X系列不用引入后面两个 -->
   <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" /> 
   <!-- 下面这个配置接口实现类与address,不要急,看下面wsdl -->
   <jaxws:endpoint 
     id="orderWS" 
     implementor="com.web.service.impl.OrderWSImpl" 
     address="/orderws">
    </jaxws:endpoint>
</beans>

【3】配置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>/*</url-pattern>
    </servlet-mapping>
</web-app>

【4】发布到tomcat


访问WSDL:

http://localhost:8080/WebServserSpringCXF2/orderws?wsdl
// /orderws前面的好理解,"/orderws"就是endpoint中定义的,"?wsdl"就不多说了


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://impl.service.web.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://service.web.com/" name="OrderWSImplService" targetNamespace="http://impl.service.web.com/">
  <wsdl:import location="http://localhost:8080/WebServserSpringCXF2/orderws?wsdl=OrderWS.wsdl" namespace="http://service.web.com/">
    </wsdl:import>
  <wsdl:binding name="OrderWSImplServiceSoapBinding" type="ns1: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:8080/WebServserSpringCXF2/orderws"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

【5】新建客户端项目 ,进行客户端编写与调试

① 新建客户端项目,根据wsdl生成Stub

过程参考:使用JDK(jetty)生成Stub进行webservice开发


② 配置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.web.service.OrderWS" 
        address= "http://localhost:8080/WebServserSpringCXF2/orderws">
    </jaxws:client>
</beans>



③ 编写客户端测试类

packagepackage com.web.Client;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.web.service.Order;
import com.web.service.OrderWS;
public class ClientTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");
        OrderWS orderWS = (OrderWS) context.getBean("orderClient");
        Order orderById = orderWS.getOrderById(1);
        System.out.println("Client "+orderById);
    }
}


运行并查看客户端输出结果:


查看服务端输出结果:


目录
相关文章
|
7月前
|
前端开发 Java 开发者
深入理解Spring Boot中的@Service注解
【4月更文挑战第22天】在 Spring Boot 应用开发中,@Service 注解扮演着特定的角色,主要用于标识服务层组件。本篇技术博客将全面探讨 @Service 注解的概念,并提供实际的应用示例,帮助开发者理解如何有效地使用这一注解来优化应用的服务层架构
1628 1
|
7月前
|
缓存 负载均衡 Java
Spring Cloud Alibaba client升级问题之升级报错如何解决
Spring Cloud Alibaba提供了一套在Spring Cloud框架基础上构建的微服务解决方案,旨在简化分布式系统的开发和管理;本合集将探讨Spring Cloud Alibaba在实际应用中的部署和使用技巧,以及该框架常见问题的诊断方法和解决步骤。
|
4月前
|
Java Spring
【Azure Service Bus】使用Spring Cloud integration示例代码,为多个 Service Bus的连接使用 ConnectionString 方式
【Azure Service Bus】使用Spring Cloud integration示例代码,为多个 Service Bus的连接使用 ConnectionString 方式
|
4月前
|
Java Spring
【Azure 服务总线】Spring Cloud 的应用 使用Service Bus 引起 org.springframework.beans.BeanInstantiationException 异常,无法启动
【Azure 服务总线】Spring Cloud 的应用 使用Service Bus 引起 org.springframework.beans.BeanInstantiationException 异常,无法启动
|
4月前
|
Java 开发工具 Spring
【Azure Spring Cloud】使用azure-spring-boot-starter-storage来上传文件报错: java.net.UnknownHostException: xxxxxxxx.blob.core.windows.net: Name or service not known
【Azure Spring Cloud】使用azure-spring-boot-starter-storage来上传文件报错: java.net.UnknownHostException: xxxxxxxx.blob.core.windows.net: Name or service not known
|
4月前
|
前端开发 JavaScript Java
【Azure 应用服务】App Service For Windows 中如何设置代理实现前端静态文件和后端Java Spring Boot Jar包
【Azure 应用服务】App Service For Windows 中如何设置代理实现前端静态文件和后端Java Spring Boot Jar包
|
4月前
|
Java Linux C++
【Azure 应用服务】App Service For Linux 部署Java Spring Boot应用后,查看日志文件时的疑惑
【Azure 应用服务】App Service For Linux 部署Java Spring Boot应用后,查看日志文件时的疑惑
|
4月前
|
Java Spring
Spring Boot Admin 查看 Client 日志
Spring Boot Admin 查看 Client 日志
71 0
|
7月前
|
JSON Java Apache
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
413 0
|
7月前
ssm(Spring+Spring mvc+mybatis)Service层实现类——DeptServiceImpl
ssm(Spring+Spring mvc+mybatis)Service层实现类——DeptServiceImpl