1.引入两个需要的jar
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.12</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.12</version> </dependency>
2.配置webservice类
package com.btw.court.webService; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface PayWebService { @WebMethod String say(@WebParam(name="arg0")String arg0); }
3.配置webservice实现类
package com.btw.court.webService.impl; import com.btw.court.webService.PayWebService; import javax.jws.WebService; @WebService(targetNamespace = "http://say.com")//对应wsdl中的命名空间xmlns:tns public class PayWebServiceImpl implements PayWebService { @Override public String say(String arg0) { return null; } }
4.配置springboot配置类
package com.btw.court.config; import com.btw.court.webService.PayWebService; import com.btw.court.webService.impl.PayWebServiceImpl; 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 { @Bean//发布服务名称,可以访问该路径查看拥有的webservice public ServletRegistrationBean dispathcherServlet() { return new ServletRegistrationBean(new CXFServlet(), "/service/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public PayWebService payWebService() { return new PayWebServiceImpl(); } /** * 绑定接口类,描述接口实现方法,以及接口名称.如果有多个接口需要实现,可以添加多个该方法(设置方法名不同,发布路径不同即可) * @return */ @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), payWebService()); endpoint.publish("/ws"); return endpoint; } }
此时,我们输入http://ip:port/service,可以看到如下图所示内容。左侧马赛克为接口包含的方法,右侧为接口的描述,包括发布地址、wsdl和命名空间。
点击wsdl的路径,可以看到接口的详细描述。