springboot使用webservice發(fā)布和調(diào)用接口的實(shí)例詳解
springboot使用webservice發(fā)布和調(diào)用接口
加入以下依賴:
<!-- cxf框架依賴 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.4</version> </dependency>
服務(wù)端service代碼:
import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; /** * TODO(描述這個類的作用) * @author zyl * @date 2019年3月27日 */ /** * 創(chuàng)建服務(wù)接口 * @author oKong * */ @WebService() public interface HelloWebService { @WebMethod public String Hello(@WebParam(name="name") String name); }
服務(wù)端實(shí)現(xiàn)類代碼:
import javax.jws.WebParam; import javax.jws.WebService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * TODO(描述這個類的作用) * @author zyl * @date 2019年3月27日 */ @WebService( targetNamespace = "demo.example.com", //wsdl命名空間 serviceName = "HelloWebService", //portType名稱 客戶端生成代碼時 為接口名稱 endpointInterface = "com.example.demo.configuraction.webservice.HelloWebService")//指定發(fā)布webservcie的接口類,此類也需要接入@WebService注解 @Configuration public class HelloWebServiceImpl implements HelloWebService{ @Override public String Hello(@WebParam(name="name") String name) { System.out.println("歡迎你"+name); return "歡迎你"+name; } }
服務(wù)端發(fā)布服務(wù)類:
我的端口設(shè)置為9999,所以我的服務(wù)地址為http://127.0.0.1:9090/ws/helloWebService?wsdl
import javax.xml.ws.Endpoint; 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.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * cxf配置類 * @author oKong * */ @Configuration public class CxfWebServiceConfig { @Autowired private Bus bus; @Autowired private HelloWebService helloWebService; @Bean("cxfServletRegistration") public ServletRegistrationBean dispatcherServlet() { //注冊servlet 攔截/ws 開頭的請求 不設(shè)置 默認(rèn)為:/services/* return new ServletRegistrationBean(new CXFServlet(), "/ws/*"); } /* * 發(fā)布endpoint */ @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(bus, helloWebService); endpoint.publish("/helloWebService");//發(fā)布地址 return endpoint; } }
客戶端調(diào)用服務(wù)代碼:
以下兩種方法選一即可
import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; /** * @ClassName:CxfClient * @Description:webservice客戶端: * 該類提供兩種不同的方式來調(diào)用webservice服務(wù) * 1:代理工廠方式 * 2:動態(tài)調(diào)用webservice * @author Jerry * @date:2018年4月10日下午4:14:07 */ public class CxfClient { public static void main(String[] args) { CxfClient.main1(); // CxfClient.main2(); } /** * 1.代理類工廠的方式,需要拿到對方的接口地址 */ public static void main1() { try { // 接口地址 String address = "http://127.0.0.1:9090/ws/helloWebService?wsdl"; // 代理工廠 JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); // 設(shè)置代理地址 jaxWsProxyFactoryBean.setAddress(address); // 設(shè)置接口類型 jaxWsProxyFactoryBean.setServiceClass(HelloWebService.class); // 創(chuàng)建一個代理接口實(shí)現(xiàn) HelloWebService us = (HelloWebService) jaxWsProxyFactoryBean.create(); // 數(shù)據(jù)準(zhǔn)備 String userId = "zz"; // 調(diào)用代理接口的方法調(diào)用并返回結(jié)果 String result = us.Hello(userId); System.out.println("返回結(jié)果:" + result); } catch (Exception e) { e.printStackTrace(); } } /** * 2:動態(tài)調(diào)用 */ public static void main2() { // 創(chuàng)建動態(tài)客戶端 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://127.0.0.1:9090/ws/helloWebService?wsdl"); // 需要密碼的情況需要加上用戶名和密碼 // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD)); Object[] objects = new Object[0]; try { // invoke("方法名",參數(shù)1,參數(shù)2,參數(shù)3....); objects = client.invoke("getUserName", "maple"); System.out.println("返回數(shù)據(jù):" + objects[0]); } catch (java.lang.Exception e) { e.printStackTrace(); } } }
到此這篇關(guān)于springboot使用webservice發(fā)布和調(diào)用接口的文章就介紹到這了,更多相關(guān)springboot webservice發(fā)布和調(diào)用接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot整合cxf發(fā)布webservice以及調(diào)用的方法
- SpringBoot調(diào)用第三方WebService接口的操作技巧(.wsdl與.asmx類型)
- SpringBoot項(xiàng)目使用?axis?調(diào)用webservice接口的實(shí)踐記錄
- webservice實(shí)現(xiàn)springboot項(xiàng)目間接口調(diào)用與對象傳遞示例
- SpringBoot調(diào)用第三方WebService接口的兩種方法
- SpringBoot調(diào)用對方webService接口的幾種方法示例
- springboot調(diào)用webservice-soap接口的實(shí)現(xiàn)
- SpringBoot調(diào)用WebService接口方法示例代碼
相關(guān)文章
SpringBoot 整合 Shiro 密碼登錄的實(shí)現(xiàn)代碼
這篇文章主要介紹了SpringBoot 整合 Shiro 密碼登錄的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02JAVA實(shí)現(xiàn)遍歷文件夾下的所有文件(遞歸調(diào)用和非遞歸調(diào)用)
本篇文章主要介紹了JAVA 遍歷文件夾下的所有文件(遞歸調(diào)用和非遞歸調(diào)用) ,具有一定的參考價值,有興趣的可以了解一下。2017-01-01深入學(xué)習(xí)spring cloud gateway 限流熔斷
這篇文章主要介紹了深入學(xué)習(xí)spring cloud gateway 限流熔斷,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04