springboot使用webservice發(fā)布和調用接口的實例詳解
更新時間:2024年10月12日 09:42:09 作者:z不像程序員的程序猿
本文介紹了如何在Springboot中使用webservice發(fā)布和調用接口,涵蓋了必要的依賴添加和代碼示例,文中提供了服務端和客戶端的實現(xiàn)方法,以及如何設置端口和服務地址,幫助讀者更好地理解和應用Springboot結合webservice的技術
springboot使用webservice發(fā)布和調用接口
加入以下依賴:
<!-- cxf框架依賴 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.4</version> </dependency>
服務端service代碼:
import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; /** * TODO(描述這個類的作用) * @author zyl * @date 2019年3月27日 */ /** * 創(chuàng)建服務接口 * @author oKong * */ @WebService() public interface HelloWebService { @WebMethod public String Hello(@WebParam(name="name") String name); }
服務端實現(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; } }
服務端發(fā)布服務類:
我的端口設置為9999,所以我的服務地址為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 開頭的請求 不設置 默認為:/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; } }
客戶端調用服務代碼:
以下兩種方法選一即可
import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; /** * @ClassName:CxfClient * @Description:webservice客戶端: * 該類提供兩種不同的方式來調用webservice服務 * 1:代理工廠方式 * 2:動態(tài)調用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(); // 設置代理地址 jaxWsProxyFactoryBean.setAddress(address); // 設置接口類型 jaxWsProxyFactoryBean.setServiceClass(HelloWebService.class); // 創(chuàng)建一個代理接口實現(xiàn) HelloWebService us = (HelloWebService) jaxWsProxyFactoryBean.create(); // 數(shù)據(jù)準備 String userId = "zz"; // 調用代理接口的方法調用并返回結果 String result = us.Hello(userId); System.out.println("返回結果:" + result); } catch (Exception e) { e.printStackTrace(); } } /** * 2:動態(tài)調用 */ 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(); } } }
到此這篇關于springboot使用webservice發(fā)布和調用接口的文章就介紹到這了,更多相關springboot webservice發(fā)布和調用接口內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關文章
SpringBoot 整合 Shiro 密碼登錄的實現(xiàn)代碼
這篇文章主要介紹了SpringBoot 整合 Shiro 密碼登錄的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02JAVA實現(xiàn)遍歷文件夾下的所有文件(遞歸調用和非遞歸調用)
本篇文章主要介紹了JAVA 遍歷文件夾下的所有文件(遞歸調用和非遞歸調用) ,具有一定的參考價值,有興趣的可以了解一下。2017-01-01