springboot 创建webservice服务接口

springboot 创建webservice服务接口

PS: 此处是在 若依前后端分离式框架 基础上增加的webservice服务接口。

pom依赖:

        <!-- CXF webservice -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.1</version>
        </dependency>
        <!-- CXF webservice -->

功能接口:

package com.ruoyi.web.webservice.service;

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

/**
 * @author gbx
 */
@WebService(targetNamespace = "http://impl.service.webservice.web.ruoyi.com")
public interface TestOneService {

    /**
     * 测试接口一
     *
     * @param data 形参
     * @return 处理结果
     */
    @WebMethod
    String apiOne(@WebParam(name = "data", targetNamespace = "http://impl.service.webservice.web.ruoyi.com",partName = "data") String data);
}

接口实现类:

package com.ruoyi.web.webservice.service.impl;

import com.ruoyi.web.webservice.service.TestOneService;
import org.springframework.stereotype.Component;

/**
 * @author gbx
 */
@Component
public class TestOneServiceImpl implements TestOneService {
    @Override
    public String apiOne(String data) {
        return "调用成功 ===> 参数data=" + data;
    }
}

webservice服务配置类:

import com.gbx.cxf.server.service.TestOneService;
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;

/**
 * 注意:
 * org.apache.cxf.Bus
 * org.apache.cxf.bus.spring.SpringBus
 * org.apache.cxf.jaxws.EndpointImpl
 * javax.xml.ws.Endpoint
 *
 * @author gbx
 */
@Configuration
public class WebServiceConfig {

    @Autowired
    private TestOneService testOneService;

    /**
     * Apache CXF 核心架构是以BUS为核心,整合其他组件。
     * Bus是CXF的主干, 为共享资源提供一个可配置的场所,作用类似于Spring的ApplicationContext,这些共享资源包括
     * WSDl管理器、绑定工厂等。通过对BUS进行扩展,可以方便地容纳自己的资源,或者替换现有的资源。默认Bus实现基于Spring架构,
     * 通过依赖注入,在运行时将组件串联起来。BusFactory负责Bus的创建。默认的BusFactory是SpringBusFactory,对应于默认
     * 的Bus实现。在构造过程中,SpringBusFactory会搜索META-INF/cxf(包含在 CXF 的jar中)下的所有bean配置文件。
     * 根据这些配置文件构建一个ApplicationContext。开发者也可以提供自己的配置文件来定制Bus。
     */
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    /**
     * 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问
     * 此方法被注释后, 即不改变前缀名(默认是services), wsdl访问地址为 http://127.0.0.1:8080/services/ws/api?wsdl
     * 去掉注释后wsdl访问地址为:http://127.0.0.1:8080/soap/ws/api?wsdl
     * http://127.0.0.1:8080/soap/列出服务列表 或 http://127.0.0.1:8080/soap/ws/api?wsdl 查看实际的服务
     * 新建Servlet记得需要在启动类添加注解:@ServletComponentScan
     * <p>
     * 如果启动时出现错误:not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet
     * 可能是springboot与cfx版本不兼容。
     * 同时在spring boot2.0.6之后的版本与xcf集成,不需要在定义以下方法,直接在application.properties配置文件中添加:
     * cxf.path=/service(默认是services)
     */
    //@Bean
    //public ServletRegistrationBean dispatcherServlet() {
    //    return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
    // }

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

启动项目访问 ===> http://localhost:8080/services,如下图:
效果图
This XML file does not appear to have any style information associated with it. The document tree is shown below.

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.webservice.web.ruoyi.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="TestOneServiceImplService" targetNamespace="http://impl.service.webservice.web.ruoyi.com">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://impl.service.webservice.web.ruoyi.com" elementFormDefault="unqualified" targetNamespace="http://impl.service.webservice.web.ruoyi.com" version="1.0">
<xs:element name="apiOne" type="tns:apiOne"/>
<xs:element name="apiOneResponse" type="tns:apiOneResponse"/>
<xs:complexType name="apiOne">
<xs:sequence>
<xs:element form="qualified" minOccurs="0" name="data" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="apiOneResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="apiOne">
<wsdl:part element="tns:apiOne" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:message name="apiOneResponse">
<wsdl:part element="tns:apiOneResponse" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:portType name="TestOneService">
<wsdl:operation name="apiOne">
<wsdl:input message="tns:apiOne" name="apiOne"> </wsdl:input>
<wsdl:output message="tns:apiOneResponse" name="apiOneResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="TestOneServiceImplServiceSoapBinding" type="tns:TestOneService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="apiOne">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="apiOne">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="apiOneResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="TestOneServiceImplService">
<wsdl:port binding="tns:TestOneServiceImplServiceSoapBinding" name="TestOneServiceImplPort">
<soap:address location="http://localhost:8080/services/ws/api"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

大功告成!

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值