1.什么是webservice?
webservice是一種遠程資源調用技術,它的實現方式主要分為兩種,
第一種是jaxws方式,它是面向方法的,它的數據類型是xml是基於soap實現傳輸;
第二種是jaxrs方式,它是面向資源的,它的數據類型是xml或json是基於http協議實現傳輸。
2.jaxws簡單應用
服務端:
第一步:創建一個簡單的web項目,略
第二步:添加webService的依賴jar包,配置web.xml
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.0.1</version> </dependency> 注:版本需與springboot對應 否則啟動就會報錯 在https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxws查找到jaxws的各種版本,進入之后可以看到對應的springboot版本

問題詳情: Description: Parameter 1 of constructor in org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration required a bean of type ‘org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath’ that could not be found. The following candidates were found but could not be injected: - Bean method ‘dispatcherServletRegistration’ in ‘DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration’ not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet Action: Consider revisiting the entries above or defining a bean of type ‘org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath’ in your configuration. Process finished with exit code 1
第三步:編寫代碼

1 import com.test.demo.service.IDataReceiverService; 2 import org.apache.cxf.Bus; 3 import org.apache.cxf.jaxws.EndpointImpl; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 8 import javax.xml.ws.Endpoint; 9 10 /** 11 * 〈cxf配置〉 12 * 默認:services 13 * http://127.0.0.1:6689/services/receiveData 14 * cxf-path配置路徑: 15 * http://127.0.0.1:6689/soap/receiveData 16 */ 17 @Configuration 18 public class CxfConfig { 19 20 @Autowired 21 private Bus bus; 22 23 @Autowired 24 private IDataReceiverService dataReceiverService; 25 26 27 /** 28 * 數據站點服務(總入口) 29 * 30 * @return 31 */ 32 @Bean 33 public Endpoint getReceiveData() { 34 EndpointImpl endpoint = new EndpointImpl(bus, dataReceiverService); 35 endpoint.publish("/receiveData"); 36 return endpoint; 37 } 38 39 }

1 package com.test.demo.service; 2 3 4 import com.test.demo.entity.ReceiveMsg; 5 6 import javax.jws.WebMethod; 7 import javax.jws.WebParam; 8 import javax.jws.WebService; 9 10 /** 11 * 接收數據處理服務類 12 * 13 * @Author: zhangt 14 * @Date: 2021/9/11 11:46 15 */ 16 17 @WebService 18 public interface IDataReceiverService { 19 20 21 /** 22 * 接收數據接口 23 * 24 * @param receiveMsg 數據 25 * @return 26 */ 27 @WebMethod 28 String getReceiveMsg(@WebParam(name = "ROOT") ReceiveMsg receiveMsg); 29 30 }

1 package com.test.demo.service.impl; 2 3 import cn.hutool.core.collection.CollUtil; 4 import cn.hutool.core.convert.Convert; 5 import com.test.demo.entity.ReceiveMsg; 6 import com.test.demo.entity.ResultMsg; 7 import com.test.demo.entity.UploadData; 8 import com.test.demo.service.IDataReceiverService; 9 import com.test.demo.util.ReceiveMsgUtil; 10 import lombok.extern.slf4j.Slf4j; 11 import org.dom4j.DocumentException; 12 import org.springframework.stereotype.Component; 13 14 import javax.jws.WebService; 15 import java.util.List; 16 import java.util.Map; 17 18 @WebService(serviceName = "receiverService", // 與接口中指定的name一致 19 targetNamespace = "http://service.demo.test.com", // 與接口中的命名空間一致,一般是接口的包名倒序排 20 endpointInterface = "com.test.demo.service.IDataReceiverService"// 接口地址 21 ) 22 @Slf4j 23 @Component 24 public class DataReceiverServiceImpl implements IDataReceiverService { 25 26 /** 27 * 接收數據接口 28 * 29 * @param receiveMsg 數據 30 * @return 31 */ 32 @Override 33 public String getReceiveMsg(ReceiveMsg receiveMsg) { 34 35 //解析報文,轉化為需要寫成文件的數據 36 UploadData uploadData = new UploadData(); 37 String dataId = receiveMsg.getDATAID(); 38 uploadData.setDataId(dataId); 39 //是否加密 (1 表示業務數據為密文傳輸,0 表示明文) 40 int sec = Convert.toInt(receiveMsg.getSEC()); 41 uploadData.setHavePwd(sec); 42 43 //數據為明文時 44 if (1 == sec) { 45 //解碼業務報文 46 try { 47 //獲取解密后的業務數據 48 String data = ReceiveMsgUtil.decodeStr(receiveMsg.getBUSINESSCONTENT()); 49 List<Map<String, Object>> list = ReceiveMsgUtil.xmlToList(data); 50 log.info("數據為list:{}", list); 51 uploadData.setBusinessData(list); 52 if (CollUtil.isEmpty(list)) { 53 return ResultMsg.getMessage(ResultMsg.BUSINESS_DATA_ANALYZE_ERROR); 54 } 55 } catch (DocumentException e) { 56 log.info("xml數據格式不正確!", e); 57 return ResultMsg.getMessage(ResultMsg.XML_ERROR); 58 } catch (Exception e) { 59 log.info("base64解碼錯!", e); 60 return ResultMsg.getMessage(ResultMsg.BASE64_ERROR); 61 } 62 } 63 64 return ResultMsg.getMessage(ResultMsg.SUCCESS); 65 // return uploadData.getReplyData(); 66 } 67 68 }

1 server: 2 port: 6689 3 spring: 4 application: 5 name: cxfDemo 6 # profiles: 7 # active: dev 8 cxf: 9 path: /soap #默認services
第四步:啟動項目,查看wsdl說明書
這里面的路徑注意cxf在web中的配置:
參數說明:
@WebService:在接口上申明,表示該接口是一個webService服務;
@WebMethod:在接口方法上聲明,表示該方法是一個webService服務方法;
@WebService(endpointInterface="cn.linc.cxf.interfaces.ServiceUI",serviceName="userService"):在該接口實現類定義;
endpointInterface:表示接口服務終端接口名;
serviceName:服務名稱;
wsdl解讀:有五個節點:服務視圖,服務協議和參數的描述,服務實現,參數描述,參數類型描述。
調用展示:
原理解析:
可以分為三個角色:服務提供者,服務消費者,服務注冊中心uddi
首先由服務提供者發布服務並且在注冊中心uddi進行注冊
然后由服務消費者訂閱服務,在注冊中心uddi上查詢到符合條件的服務
服務注冊中心返回服務條件服務的描述文件給服務訂閱者
訂閱者根據描述文件進行服務端的服務調用,並返回數據。
工具:
加解密:https://tool.oschina.net/encrypt
soapUI:https://www.soapui.org/downloads/soapui/
參考:
https://blog.csdn.net/qiaodaima0/article/details/100765613
https://www.cnblogs.com/xiechenglin/p/10500558.html
https://www.cnblogs.com/shuaijie/articles/5913750.html