springboot使用webservice發(fā)布和調(diào)用接口的實例詳解
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>
服務端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ā)布服務類:
我的端口設(shè)置為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 開頭的請求 不設(shè)置 默認為:/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)用服務代碼:
以下兩種方法選一即可
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服務
* 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)建一個代理接口實現(xiàn)
HelloWebService us = (HelloWebService) jaxWsProxyFactoryBean.create();
// 數(shù)據(jù)準備
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項目使用?axis?調(diào)用webservice接口的實踐記錄
- webservice實現(xiàn)springboot項目間接口調(diào)用與對象傳遞示例
- SpringBoot調(diào)用第三方WebService接口的兩種方法
- SpringBoot調(diào)用對方webService接口的幾種方法示例
- springboot調(diào)用webservice-soap接口的實現(xiàn)
- SpringBoot調(diào)用WebService接口方法示例代碼
相關(guān)文章
JFileChooser實現(xiàn)對選定文件夾內(nèi)圖片自動播放和暫停播放實例代碼
這篇文章主要介紹了JFileChooser實現(xiàn)對選定文件夾內(nèi)圖片自動播放和暫停播放實例代碼,需要的朋友可以參考下2017-04-04
spring boot mybatis多數(shù)據(jù)源解決方案過程解析
這篇文章主要介紹了spring boot mybatis多數(shù)據(jù)源解決方案過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11
SpringBoot環(huán)境搭建及第一個程序運行(小白教程)
這篇文章主要介紹了SpringBoot環(huán)境搭建及第一個程序運行,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
Spring boot集成redis lettuce代碼實例
這篇文章主要介紹了Spring boot集成redis lettuce代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04
將java文件轉(zhuǎn)變?yōu)閎at文件實現(xiàn)方式
文章介紹了如何將Java文件轉(zhuǎn)換為批處理文件(.bat),以便于快速運行,步驟包括刪除包聲明、編譯生成.class文件,然后編寫批處理文件指定運行命令2026-01-01

