Springboot实现webservice

本地JDK:1.8
准备工作:添加CXF的依赖

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.2.4</version>
</dependency>
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>

一.定义要发布的接口和实现类

接口:

@WebService(targetNamespace = "http://webService.entryinterface.ruijie.com")
public interface QisService {

    @WebMethod
    String pattern(@WebParam(name = "xml") String xml) throws Exception;

}

实现类:

// serviceName=对外发布的服务名,targetNamespace=指定你想要的名称空间,通常使用使用包名反转,endpointInterface=服务接口全路径
@WebService(serviceName = "QisService", targetNamespace = "http://webService.entryinterface.ruijie.com", endpointInterface = "com.ruijie.entryinterface.webService.QisService")
@Component
public class QisServiceImpl implements QisService {

    @Override
    public String pattern(String xml) throws Exception {
        ...
        return null;
    }
}

定义配置类:

@Configuration
public class CxfConfig {

    @Autowired
    private Bus bus;

    @Autowired
    QisService qisService;

    /**
     * 默认servlet路径/*,如果覆写则按照自己定义的来
     *
     * @return
     */
    @SuppressWarnings("all")
    @Bean
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/pattern/*");
    }

    @Bean
    ServletWebServerFactory servletWebServerFactory() {
        return new TomcatServletWebServerFactory();
    }

    /**
     * JAX-WS 站点服务
     **/
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, qisService);
        endpoint.publish("/qis");
        return endpoint;
    }
}

启动完成后,输入地址:http://127.0.0.1:8080/pattern/qis?wsdlSpringboot实现webservice_第1张图片

你可能感兴趣的:(SpringBoot)