2019-04-13:项目中需要用WebService,现在用springboot整合WebService比以前方便多了,发个例子出来

1.pom.xml中引入依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>
    <!-- apache-cxf -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.1.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>3.1.6</version>
    </dependency>

2.新建webservice接口

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

/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.9-b130926.1035
 * Generated source version: 2.2
 * 
 */
@WebService

public interface WebServiceBpmService {

    /**
     * 
     * @param name
     * @return
     *     returns java.lang.String
     */

    @WebMethod
    String toData(@WebParam(name = "name")String name);
}

3.webservice接口实现

import org.springframework.stereotype.Service;

import javax.jws.WebService;

/**
 * @author gaorui
 * @version 1.0
 * @className WebServiceBpmServiceImpl
 * @description
 * @date 2019/7/23 10:51
 */

@Service
@WebService(serviceName = "WebServiceBpmService", // 与接口中指定的name一致
        targetNamespace = "http://webservice.onestop.tnzosoft.com", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.tnzosoft.onestop.webservice.WebServiceBpmService" // 接口地址
)
public class WebServiceBpmServiceImpl implements WebServiceBpmService {

    @Override
    public String toData(String name) {
        if(name !=null && name.length()>0){
            String[] aa = name.split(",");
            for(int i=0;i<aa.length;i++){
                System.out.println(aa[i]);
            }
        }
        return "返回数据~~~~"+name;
    }

}

4.新建配置类

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;

/**
 * @author gaorui
 * @version 1.0
 * @className WebServiceConfig
 * @description
 * @date 2019/7/23 10:58
 */

@Configuration
public class WebServiceConfig {

    @Autowired
    private WebServiceBpmService webServiceBpmService;

    /**
     * 注入servlet  bean name不能dispatcherServlet 否则会覆盖dispatcherServlet
     * @return
     */
    @Bean(name = "cxfServlet")
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
    }

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

    /**
     * 注册WebServiceDemoService接口到webservice服务
     * @return
     */
    @Bean(name = "WebServiceBpmEndpoint")
    public EndpointImpl sweptPayEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), webServiceBpmService);
        endpoint.publish("/webservice");
        return endpoint;
    }
}

5.启动项目后,访问 http://localhost:8080/webservice
[图片上传失败...(image-a99c4a-1578470307733)]

wsdl文档地址为:http://localhost:8080/webservice/webservice?wsdl

webservice服务已完成

6.webservice调用
idea中右键点击项目 --> webservices --> Generate Java Code From Wsdl
[图片上传失败...(image-ec6434-1578470307733)]

点OK后会在Package prefix指定的目录下生成相关类文件

7.测试接口

package com;

/**
 * @author gaorui
 * @version 1.0
 * @className test
 * @description
 * @date 2019/7/23 11:43
 */
public class test {
    public static void main(String[] args) {
        WebServiceBpmService_Service webServiceBpmService_service = new WebServiceBpmService_Service();
        WebServiceBpmService webServiceBpmService = webServiceBpmService_service.getWebServiceBpmServiceImplPort();
        String result = webServiceBpmService.hello("1,2,3,4,5,6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1");
        System.out.println("================"+result);
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容