1、POM依赖项

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

2、服务接口

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

@WebService(name = "ServerServiceDemo", targetNamespace = "http://server.webservice.example.com")
public interface ServerServiceDemo {

    @WebMethod
    String emrService(@WebParam String data);
}

3、服务接口实现

@Component
@WebService(name = "ServerServiceDemo", targetNamespace = "http://server.webservice.example.com",
        endpointInterface = "com.insight.hr.webservice.ServerServiceDemo")
public class ServerServiceDemoImpl implements ServerServiceDemo {
    @Override
    public String emrService(String data) {
        return "调用成功";
    }
}

4、配置类

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import javax.xml.ws.Endpoint;

@Configuration
public class WebServiceConfig {

    @Autowired
    private Bus bus;
    @Autowired
    private ServerServiceDemo serverServiceDemo;

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

}

5、查看服务是否成功启动

踩坑

错误:Could not load extension class org.apache.cxf.ws.policy.AssertionBuilderRegistryImpl.

排查后发现是 Jar 冲突,将冲突 Jar 里的neethi旧版本包排除掉解决

<exclusion>
    <artifactId>neethi</artifactId>
    <groupId>org.apache.neethi</groupId>
</exclusion>
错误:nested exception is java.lang.NoSuchMethodError: org.apache.ws.commons.schema.XmlSchemaCollection.read(Lorg/w3c/dom/Document;Ljava/lang/String;)Lorg/apache/ws/commons/schema/XmlSchema;

排查后依然是 Jar 冲突,将冲突 Jar 里的ws.commons.schema旧版本包排除掉解决

<exclusion>
    <artifactId>XmlSchema</artifactId>
    <groupId>org.apache.ws.commons.schema</groupId>
</exclusion>