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的路徑,可以看到接口的詳細描述。
