「File」→「New」→「Project」→「Spring Initializr」……java
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.6</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.6</version> </dependency>
這個配置根據實際需求添加,若是不修改server.port,服務器會默認從8080端口啓動,爲避免衝突,這裏設置服務端口爲8090。web
server.port=8090
import javax.jws.WebService; @WebService public interface DemoService { public String sayHello(String user); }
import java.util.Date; public class DemoServiceImpl implements DemoService { @Override public String sayHello(String user) { return user+":hello"+"("+new Date()+")"; } }
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 CxfConfig { @Bean public ServletRegistrationBean dispatcherServlet() { return new ServletRegistrationBean(new CXFServlet(),"/demo/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public DemoService demoService() { return new DemoServiceImpl(); } @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), demoService()); endpoint.publish("/api"); return endpoint; } }
直接啓動WebserviceApplication,看到服務器正常啓動
spring
在瀏覽器輸入http://localhost:8090/demo/ 能夠看到系統提供的webservice服務
apache
Web service wsdl url 填入服務端WSDL地址
若是使用的是JDK1.8可能會有bug,生成時報錯:因爲 accessExternalSchema 屬性設置的限制而不容許 ‘file’ 訪問, 所以沒法讀取方案文檔 ‘xjc.xsd’。api
org.xml.sax.SAXParseException; systemId: jar:file:/D:/apache-cxf-2.7.11/apache-cxf-2.7.11/lib/jaxb-xjc2.2.6.jar!/com/sun/tools/xjc/reader/xmlschema/bindinfo/binding.xsd; lineNumber: 52; columnNumber: 88; schema_reference: 因爲 accessExternalSchema 屬性設置的限制而不容許 'file' 訪問, 所以沒法讀取方案文檔 'xjc.xsd'。
解決方法:
在jdk的安裝路徑下,如 C:\Java\jdk1.8.0_65\jre\lib,添加一個屬性文件jaxp.properties,並寫上以下內容javax.xml.accessExternalSchema = all瀏覽器
import mypackage.DemoServiceImplService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class WebserviceApplicationTests { @Test public void contextLoads() { DemoServiceImplService webServiceImpl = new DemoServiceImplService(); String result = webServiceImpl.getDemoServiceImplPort().sayHello("沒有說"); System.out.println("==========================================="); System.out.println(result); System.out.println("==========================================="); } }
執行結果:
服務器
成功集成cxf後,發現只有webservice服務能夠正常使用,其餘請求url所有沒法正常訪問。併發
而後在cxf 配置文件中app
WebServiceCxfCfg.java
更改此方法名:public ServletRegistrationBean dispatcherServlet() frontend
@Bean public ServletRegistrationBean disServlet(){ return new ServletRegistrationBean(new CXFServlet() , "/services/*"); }
便可成功訪問其餘url
評論中說的是public ServletRegistrationBean dispatcherServlet() 把默認映射覆蓋掉了,把這個名字改掉,控制類方法就能訪問了。
更改此方法明後能夠正常其餘請求url,webservice服務也正常。