栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

WebService整合SpringBoot2.0

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

WebService整合SpringBoot2.0

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

WebService整合SpringBoot2.0
  • 一、生产者
    • 1、依赖
    • 2、被调用方法
    • 3、配置类
    • 4、调用地址
    • 5、启动项目
  • 二、消费者
    • 1、依赖
    • 2、代码
    • 3、效果


一、生产者 1、依赖
  • spring-boot-starter-web-services:
    这个没啥好说的吧,你springboot集成webservice,不加入这个包,难道加个什么quartz包?
  • cxf-spring-boot-starter-jaxws
    这个包是给webservice发布使用的。
    我们知道 Web Service是一种能够使应用程序在不同的平台使用不同的编程语言进行通讯的技术规范,这种技术规范的实现方式是通过基于XML形式的协议(SOAP)进行通讯或者说是RESTFUL形式的。
    如果使用这两种方式进行通讯,那么必须要有规范化的工作,也就是jaxws规范和 jar-rs规范。
    而cxf正好是这两种规范的具体实现方式。

换句话说:JAX-WS是标准,CXF与Axis则是具体的框架实现。

        
            org.springframework.boot
            spring-boot-starter-web-services
        

        
            org.apache.cxf
            cxf-spring-boot-starter-jaxws
            3.2.4
        

        
            org.hibernate
            hibernate-validator
            5.2.4.Final
        
2、被调用方法

@WebService:标明是个webservice服务,发布的时候会带上这个类。
@WeBMethod:webservice中发布的方法
@WebParam:对参数的别名,不写也不影响,但是参数在wsdl中看起来是arg0,不利于理解。

package com.example.myspringboot2.service;

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


@WebService(targetNamespace = "http://myspringboot2.example.com/")// 由于不同包所以要在接口和实现类上各加一个targetNamespace ,命名空间,一般是接口的包名倒序)
public interface AobingService {

    @WebMethod
    String hello(String name);
}

package com.example.myspringboot2.service.impl;

import com.example.myspringboot2.service.AobingService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.jws.WebService;


@WebService(serviceName = "AobingService", // 与接口中指定的name一致
        targetNamespace = "http://myspringboot2.example.com",// 命名空间,一般是接口的包名倒序
        endpointInterface = "com.example.myspringboot2.service.AobingService"// 接口地址
)
@Service
public class AobingServiceImpl implements AobingService {

    @Override
    public String hello(String name) {
        return "Yo man Hello,I am" + name;
    }
}
3、配置类
package com.example.myspringboot2.config;

import javax.xml.ws.Endpoint;

import com.example.myspringboot2.service.AobingService;
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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.cxf.transport.servlet.CXFServlet;

import javax.xml.ws.Endpoint;




@Configuration
public class WebServiceConfig {

    @Autowired
    private AobingService aobingService;

    // 此方法被注释后:wsdl访问地址为http://127.0.0.1:9090/webservice?wsdl
    // 去掉注释后:wsdl访问地址为:http://127.0.0.1:9090/webservice/aobingService?wsdl
    @Bean(name = "cxfServlet")
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/webservice
    @Bean(name = "webServiceDemoEndPoint")
    public Endpoint webServiceDemoEndPoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), aobingService);
        endpoint.publish("/aobingService");
        return endpoint;
    }

}

4、调用地址

http://127.0.0.1:9090/webservice/aobingService?wsdl

方法详情
http://127.0.0.1:9090/webservice/aobingService?wsdl

5、启动项目

二、消费者 1、依赖
        
            org.apache.cxf
            cxf-spring-boot-starter-jaxws
            3.2.4
        
2、代码
package com.example.myspringboot2.controller;


import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import javax.xml.namespace.QName;


public class AobingGetController {

    public static final String URL = "http://127.0.0.1:9090/webservice/aobingService?wsdl";
    public static final String NAMESPACE_URL = "http://myspringboot2.example.com/";
    public static final String METHOD_NAME = "hello";
    public static final String PARAM_STR = "渊哥";

    public static void main(String[] args) {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        // 创建动态客户端, URL -> 接口地址
        Client client = dcf.createClient(URL);
        // 创建QName, NAMESPACE_URL -> 命名空间,METHOD_NAME -> 方法名
        QName qName = new QName(NAMESPACE_URL, METHOD_NAME);
        try {
            // 接口调用  PARAM_STR -> xml参数字符串
            Object[] objects = client.invoke(qName, PARAM_STR);
            // 返回的数据
            System.out.println(objects[0].toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

3、效果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/601647.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号