Springboot集成webservice案例

Springboot整合CXF实现webservice服务,配置多个接口,如果是调试IPTV相关的的接口,请参考:https://blog.csdn.net/dong945221578/article/details/71429735

代码清单:
pom.xml

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--webservice依赖-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.5</version>
            <exclusions>
                <exclusion>
                    <artifactId>cxf-core</artifactId>
                    <groupId>org.apache.cxf</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.2.6</version>
        </dependency>
         <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.4.1</version>
        </dependency>
    </dependencies>

WebServiceMsgService.java 接口

package cn.tugos.webservice.demo.service;

import cn.tugos.webservice.demo.vo.ResultVO;

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

/**
 * @author qinming
 * @date 2020-12-10 10:27:21
 * <p> 无 </p>
 */
@WebService
public interface WebServiceMsgService {
    
    

    @WebMethod
    ResultVO sendMsg(@WebParam(name = "name") String name,@WebParam(name = "message") String message);


}

WebServiceMsgServiceImpl.java 实现类

package cn.tugos.webservice.demo.service;

import cn.tugos.webservice.demo.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.jws.WebService;

/**
 * @author qinming
 * @date 2020-12-10 10:28:06
 * <p> 无 </p>
 */
@WebService(serviceName = "messageService",//对外发布的服务名
        targetNamespace = "http://demo.webservice.tugos.cn",//指定你想要的名称空间,通常使用使用包名反转
        endpointInterface = "cn.tugos.webservice.demo.service.WebServiceMsgService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
@Service
@Slf4j
public class WebServiceMsgServiceImpl implements WebServiceMsgService {
    
    

    @Override
    public ResultVO sendMsg(String name, String message) {
    
    
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(0);
        resultVO.setMessage("name:"+name+",message:"+message);
        return resultVO;
    }
}

WebServiceNotifyService.java 回调接口

package cn.tugos.webservice.demo.service;

import cn.tugos.webservice.demo.vo.ResultVO;

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

/**
 * @author qinming
 * @date 2020-12-14 19:24:44
 * <p> 无 </p>
 */
@WebService
public interface WebServiceNotifyService {
    
    

    @WebMethod
    ResultVO notify(@WebParam(name = "userId") Integer userId, @WebParam(name = "message") String message);


}

WebServiceNotifyServiceImpl.java 回调接口实现

package cn.tugos.webservice.demo.service;

import cn.tugos.webservice.demo.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.jws.WebService;

/**
 * @author qinming
 * @date 2020-12-14 19:26:49
 * <p> 无 </p>
 */
@WebService(serviceName = "notifyService",//对外发布的服务名
        targetNamespace = "http://demo.webservice.tugos.cn",//指定你想要的名称空间,通常使用使用包名反转
        endpointInterface = "cn.tugos.webservice.demo.service.WebServiceNotifyService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
@Service
@Slf4j
public class WebServiceNotifyServiceImpl implements WebServiceNotifyService{
    
    

    @Override
    public ResultVO notify(Integer userId, String message) {
    
    
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(0);
        resultVO.setMessage("userId:"+userId+",message:"+message);
        return resultVO;
    }
}

WebServiceConfig.java webservice配置

package cn.tugos.webservice.demo.config;

import cn.tugos.webservice.demo.service.WebServiceMsgService;
import cn.tugos.webservice.demo.service.WebServiceNotifyService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
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 javax.xml.ws.Endpoint;
/**
 * @author qinming
 * @date 2020-12-10 10:29:22
 * <p> webservice配置 </p>
 */
@Configuration
public class WebServiceConfig {
    
    

    @Autowired
    private WebServiceMsgService webServiceMsgService;

    @Autowired
    private WebServiceNotifyService webServiceNotifyService;

    /**
     * 配置访问根路径
     * 未设置默认为 http://localhost:8080/services
     * 自定义设置: http://localhost:8080/myService
     */
    @SuppressWarnings("all")
    @Bean(name = "cxfServlet")
    public ServletRegistrationBean cxfServlet() {
    
    
        return new ServletRegistrationBean(new CXFServlet(), "/myService/*");
    }

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

    @Bean(name = "webServiceDemoEndPoint")
    public Endpoint webServiceDemoEndPoint() {
    
    
        EndpointImpl endpoint = new EndpointImpl(springBus());
        //配置webservice服务接口 接口地址,对应的接口
        Endpoint.publish("/msgService",webServiceMsgService);
        Endpoint.publish("/notifyService",webServiceNotifyService);
        return endpoint;
    }

}

TestDemo.java 测试类,提供了两种SOAP协议接口调用方法

package cn.tugos.webservice.demo.test;

import cn.hutool.http.webservice.SoapClient;
import cn.hutool.json.JSONUtil;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import javax.xml.namespace.QName;
import java.util.HashMap;
import java.util.Map;

/**
 * @author qinming
 * @date 2020-12-14 19:51:03
 * <p> 测试webservice </p>
 */
public class TestDemo {
    
    


    public static void main(String[] args) throws Exception {
    
    
        String url = "http://localhost:8080/myService/msgService?wsdl";
        Object[] str = {
    
    "qinming", "你好啊"};
        String method = "sendMsg";
        dcfSoap(url, method, str);

        HashMap<String, Object> params = new HashMap<>(16);
        params.put("userId", 666);
        params.put("message", "你好!666");
        //方法要带前缀,命名空间最后的斜杠要带上
        soapUtil("http://localhost:8080/myService/notifyService?wsdl",
                "web:notify", "http://service.demo.webservice.tugos.cn/", params);

    }


    /**
     * 动态调用webservice
     */
    public static void dcfSoap(String url, String method, Object[] str) throws Exception {
    
    
        /// 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(url);
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
        QName name = new QName("http://service.demo.webservice.tugos.cn/", method);
        HashMap<String, Object> map = new HashMap<>(16);
        try {
    
    
            // invoke("方法名",参数1,参数2,参数3....);
            Object[] objects = client.invoke(name, str);
            for (Object object : objects) {
    
    
                System.out.println(JSONUtil.toJsonStr(object));
            }
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }


    /**
     * 通过hutool根据类调用
     */
    public static void soapUtil(String url, String method, String nameSpace, Map<String, Object> params) {
    
    
        //接口地址
        SoapClient soapClient = SoapClient.create(url);
        //调用的方法,命名空间
        soapClient.setMethod(method, nameSpace);
        //false不带前缀
        soapClient.setParams(params, false);
        //格式化返回
        String send = soapClient.send(true);
        System.out.println(send);
    }
}

源码地址:https://github.com/qinming99/tugos-demo

猜你喜欢

转载自blog.csdn.net/qq_33505611/article/details/111301834