springboot集成webservice接口

引入jar

    
        1.8
        1.8
        2.6.3
        2021.0.1
        2021.0.1.0
    

    
    
        
            
                org.springframework.boot
                spring-boot-starter-parent
                ${spring.boot.version}
                pom
                import
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring.cloud.version}
                pom
                import
            
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                ${spring.cloud.alibaba.version}
                pom
                import
            
        
    

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

        
        
            com.alibaba.cloud
            spring-cloud-starter-stream-rocketmq
        
        
            org.projectlombok
            lombok
            1.18.12
            compile
        
        
        
            org.springframework.boot
            spring-boot-starter-web-services
        
        
            org.apache.cxf
            cxf-spring-boot-starter-jaxws
            3.5.2
        
        
    

新建接口类

package com.liu.test.ws;



import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
 * @author liu
 * @date 2022年05月06日 17:05
 */
@WebService
public interface IRegisterApiService {
    @WebMethod
    public String  getConfig(@WebParam(name = "verificationCode", targetNamespace = "http://webservice.service.liu.com/") String verificationCode);
}

新建接口实现类

package com.liu.test.ws;

/**
 * @author liu
 * @date 2022年05月06日 17:06
 */

import org.springframework.stereotype.Service;

import javax.jws.WebService;


@WebService(serviceName = "IRegisterApiService", targetNamespace = "http://webservice.service.liu.com/",
        endpointInterface = "com.liu.test.ws.IRegisterApiService"
)
@Service
public class RegisterApiServiceImpl implements IRegisterApiService {

    @Override
    public String  getConfig(String verificationCode) {

        return "发布成功";
    }
}

新建配置类

package com.liu.test.ws;


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 liu
 * @date 2022年05月06日 17:02
 */
@Configuration
public class WebServiceConfig {

    @Autowired
    private IRegisterApiService registerApiService;

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

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

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

启动类

启动

访问http://localhost:9091/ws
此处的ws为配置文件中

在这里插入图片描述

http://localhost:9091/ws/getName?wsdl
springboot集成webservice接口_第1张图片

你可能感兴趣的:(项目实战,spring,boot,java,spring)