SpringBoot整合WebServices(Apache CXF JAX-WS)(一)入门示例

目录

一、简介

Web Services 简介

soap协议

Apache CXF

二、WebService框架

Apache CXF

Apache axis2

Spring Web Services

三、Apache CXF JAX-WS示例

1.创建SpringBoot项目并添加jaxws依赖

扫描二维码关注公众号,回复: 12975092 查看本文章

2.创建接口

3.WebServiceConfig配置类

4.启动项目

5.测试WebService

(1)使用idea生成客户端

(2)测试


一、简介

Web Services 简介

Web Services 属于 RPC 的一种。Web Services 使用 SOAP 作为传输协议,使用 WSDL 作为服务的描述语言,使用 UDDI 作为服务注册发现(虽然没有发展起来)。虽然 Web Services 的相关协议在 2007 年之后基本就没再更新,但是在一些银行等金融机构,Web Services 还在被大量使用。WebService 的框架很多,比如 Axis2,XFire,CXF 等等。Apache Cxf 是其中最优秀,最具有生命力的一个,而且 CXF 框架不仅仅是一个 Web Services 框架,甚至可以通过 CXF 来实现 RESTful 风格的服务。

soap协议

简单对象访问协议(Simple Object Access Protocol,SOAP)是一种基于 XML 的协议,可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议(HTTP),简单邮件传输协议(SMTP),多用途网际邮件扩充协议(MIME),基于“通用”传输协议是 SOAP的一个优点。

Apache CXF

JAX-WS 全称是JavaTM API for XML-Based Web Services 

AX-RS  全称是JavaTM API for RESTful Web Services

二、WebService框架

比较流行的WebService框架有如下几种:

Apache CXF

http://cxf.apache.org/docs/springboot.html

Apache axis2

http://axis.apache.org/axis2/java/core/

Spring Web Services

https://docs.spring.io/spring-ws/docs/3.0.10.RELEASE/reference/#resources

三、Apache CXF JAX-WS示例

1.创建SpringBoot项目并添加jaxws依赖

SpringBoot版本2.2.4.RELEASE

      <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.3.9</version>
        </dependency>

如果发布失败,注意版本差距不能太大

https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxws/3.3.9

2.创建接口

ServiceDemo
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(name = "ServiceDemo",
        targetNamespace = "http://www.baidu.com")
public interface ServiceDemo {

    @WebMethod
    String emrService(@WebParam String data);

}

 ServiceDemoImpl

import org.springframework.stereotype.Component;
import javax.jws.WebParam;
import javax.jws.WebService;

@Component
@WebService(name = "ServiceDemo",
        targetNamespace = "http://www.baidu.com",
        endpointInterface = "com.asyf.demo.service.ServiceDemo")
public class ServiceDemoImpl implements ServiceDemo {

    @Override
    public String emrService(@WebParam String data) {
        if (null == data || "".equals(data.trim())) {
            return "传入的参数为空";
        }
        return "调用成功,传入的参数是:" + data;
    }

}

3.WebServiceConfig配置类

import com.asyf.demo.service.ServiceDemo;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;

@Configuration
public class WebServiceConfig {

    @Autowired
    private ServiceDemo serviceDemo;

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), serviceDemo);
        endpoint.publish("/ws/api");
        return endpoint;
    }

}

4.启动项目

服务发布成功访问http://localhost:8080/services/ws/api?wsdl可以看到如图所示的页面

5.测试WebService

(1)使用idea生成客户端

新建client包存放生成的文件,右键可以看到WebServices

点击ok生成客户端文件

成功生成客户端文件

(2)测试

使用main函数测试

  public static void main(String[] args) {
        String address = "http://127.0.0.1:8080/services/ws/api?wsdl";
        // 代理工厂
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        // 设置代理地址
        jaxWsProxyFactoryBean.setAddress(address);       
        // 设置接口类型
        jaxWsProxyFactoryBean.setServiceClass(ServiceDemo.class);
        // 创建一个代理接口实现
        ServiceDemo cs = (ServiceDemo) jaxWsProxyFactoryBean.create();
        // 数据准备
        String data = String.valueOf(new Date());
        // 调用代理接口的方法调用并返回结果
        String rs = cs.emrService(data);
        System.out.println("==============\n返回结果:" + rs);
    }

 控制台打印

猜你喜欢

转载自blog.csdn.net/cs373616511/article/details/112754986