现在一般都流行用微服务了,但有时候要和第三方对接时,偶尔会用到WebService。这里演示下如何与cxf进行整合,并进行WebService服务发布
1、首先在POM中添加以下依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.1.11</version>
</dependency>
2、进行WebService接口编写
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface CalculateService {
@WebMethod
int add(int a, int b);
}
3、进行WebService接口的实现编写
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class CalculateServiceImpl implements CalculateService {
@WebMethod
@Override
public int add(int a, int b) {
return a + b;
}
}
4、建立Webservice服务配置类
import com.company.cxf.service.CalculateService;
import com.company.cxf.service.impl.CalculateServiceImpl;
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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration
public class WebServiceConfig {
/**
* 发布服务名称,要注意这里方法名称不能用dispatcherServlet,否则会导致Controller服务无法正常响应
*/
@Bean
public ServletRegistrationBean wsDispatcherServlet(){
return new ServletRegistrationBean(new CXFServlet(),"/service/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus()
{
return new SpringBus();
}
@Bean
public CalculateService userService()
{
return new CalculateServiceImpl();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint=new EndpointImpl(springBus(), userService());//绑定要发布的服务
endpoint.publish("/calculate"); //显示要发布的名称
return endpoint;
}
5、然后启动服务后,访问http://localhost:8080/service/calculate?wsdl。
可以用自己写个WebService客户端程序进行测试,或者用SOAP UI进行测试。