1:添加依賴
Maven工程:java
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.3</version> </dependency>
Gradle工程:git
'org.apache.cxf:cxf-spring-boot-starter-jaxws:3.3.3'
2:服務端接口github
package com.nobody.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; /** * 測試接口 * * @author Μr.ηobοdy * * @date 2019-12-29 * */ @WebService(name = "UserService", // 暴露服務名稱 targetNamespace = "http://service.nobody.com" // 命名空間,通常是接口的包名倒序 ) public interface UserService { @WebMethod @WebResult(name = "String", targetNamespace = "") String addUser(@WebParam(name = "username") String username, @WebParam(name = "age") int age); }
3:服務端接口實現web
package com.nobody.service.impl; import javax.jws.WebService; import org.springframework.stereotype.Component; import com.nobody.service.UserService; /** * 測試接口實現 * * @author Μr.ηobοdy * * @date 2019-12-29 * */ @WebService(serviceName = "UserService", // 與接口中指定的name一致 targetNamespace = "http://service.nobody.com", // 與接口中的命名空間一致,通常是接口的包名倒 endpointInterface = "com.nobody.service.UserService" // 接口地址 ) @Component public class UserServiceImpl implements UserService { @Override public String addUser(String username, int age) { return "Add user success,username:" + username + ",age:" + age; } }
4:CXF配置spring
package com.nobody.config; import javax.xml.ws.Endpoint; import org.apache.cxf.Bus; import org.apache.cxf.jaxws.EndpointImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.nobody.service.UserService; /** * cxf配置 * * @author Μr.ηobοdy * * @date 2019-12-29 * */ @Configuration public class CxfConfig { @Autowired private Bus bus; @Autowired private UserService userService; @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(bus, userService); endpoint.publish("/UserService"); return endpoint; } }
5:wsdl文件
默認服務在host:port/工程服務名/services/所在的路徑下。由於本工程沒設置服務名,故此接口發佈路徑爲/services/UserService,wsdl文件路徑爲:apache
http://localhost:8080/services/UserService?wsdl
服務啓動後,在瀏覽器輸入以上地址便可看到wsdl文件:
6:客戶端調用瀏覽器
package com.nobody.controller; import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("demo") public class DemoController { @GetMapping("test") public String test(@RequestParam String username, @RequestParam int age) { String result = null; // 建立動態客戶端 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://localhost:8080/services/UserService?wsdl"); // 若是須要密碼時加上用戶名和密碼 // client.getOutInterceptors().add(new ClientLoginInterceptor(USERNAME,PASSWORD)); // 接受返回值對象 Object[] objects = new Object[0]; try { // 調用 objects = client.invoke("addUser", username, age); result = "調用webservice接口返回數據:" + objects[0]; } catch (Exception e) { e.printStackTrace(); } return result; } }
7:啓動工程,在瀏覽器輸入如下地址便可進行調用app
http://127.0.0.1:8080/demo/test?username=Mr.nobody&age=18
返回結果:
8:項目Github下載地址
Springboot整合cxf使用WebServiceide
本文同步分享在 博客「Μr.ηobοdy」(CSDN)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。svg