From a6ef6636864475e94fc41381b6b6aecb95e753e3 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Wed, 18 Dec 2024 10:09:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=AE=8C=E5=96=84webservice?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ktg-admin/pom.xml | 21 ----------- ktg-mes/pom.xml | 6 ++++ .../ktg/mes/md/config/WebServiceConfig.java | 36 +++++++++++++++++++ .../md/service/IMasterDataSyncService.java | 19 +++++++--- .../impl/MasterDataSyncServiceImpl.java | 27 ++++++++++---- 5 files changed, 77 insertions(+), 32 deletions(-) create mode 100644 ktg-mes/src/main/java/com/ktg/mes/md/config/WebServiceConfig.java diff --git a/ktg-admin/pom.xml b/ktg-admin/pom.xml index 93677f9..246b229 100644 --- a/ktg-admin/pom.xml +++ b/ktg-admin/pom.xml @@ -124,27 +124,6 @@ jtds 1.3.1 - - - org.springframework.boot - spring-boot-starter-web-services - - - - org.springframework - spring-oxm - - - - org.apache.cxf - cxf-spring-boot-starter-jaxws - 4.0.3 - - - jakarta.jws - jakarta.jws-api - 2.1.0 - diff --git a/ktg-mes/pom.xml b/ktg-mes/pom.xml index 2dea2b1..2b4f85f 100644 --- a/ktg-mes/pom.xml +++ b/ktg-mes/pom.xml @@ -66,6 +66,12 @@ org.projectlombok lombok + + + org.apache.cxf + cxf-spring-boot-starter-jaxws + 3.4.4 + \ No newline at end of file diff --git a/ktg-mes/src/main/java/com/ktg/mes/md/config/WebServiceConfig.java b/ktg-mes/src/main/java/com/ktg/mes/md/config/WebServiceConfig.java new file mode 100644 index 0000000..463c196 --- /dev/null +++ b/ktg-mes/src/main/java/com/ktg/mes/md/config/WebServiceConfig.java @@ -0,0 +1,36 @@ +package com.ktg.mes.md.config; + +import com.ktg.mes.md.service.IMasterDataSyncService; +import lombok.RequiredArgsConstructor; +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.boot.web.servlet.ServletRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.xml.ws.Endpoint; + +@Configuration +@RequiredArgsConstructor +public class WebServiceConfig { + private final IMasterDataSyncService masterDataSyncService; + + @Bean(name = Bus.DEFAULT_BUS_ID) + public SpringBus springBus() { + return new SpringBus(); + } + + @Bean + public ServletRegistrationBean cxfServlet() { + return new ServletRegistrationBean<>(new CXFServlet(), "/WebServices/open/*"); + } + + @Bean + public Endpoint endpoint() { + EndpointImpl endpoint = new EndpointImpl(springBus(), masterDataSyncService); + endpoint.publish("/MasterDataSyncService"); + return endpoint; + } +} diff --git a/ktg-mes/src/main/java/com/ktg/mes/md/service/IMasterDataSyncService.java b/ktg-mes/src/main/java/com/ktg/mes/md/service/IMasterDataSyncService.java index 6cb6cd1..8b2021b 100644 --- a/ktg-mes/src/main/java/com/ktg/mes/md/service/IMasterDataSyncService.java +++ b/ktg-mes/src/main/java/com/ktg/mes/md/service/IMasterDataSyncService.java @@ -2,29 +2,40 @@ package com.ktg.mes.md.service; import javax.jws.WebMethod; import javax.jws.WebParam; +import javax.jws.WebResult; import javax.jws.WebService; import javax.swing.*; -@WebService(name = "masterDataSyncService", targetNamespace = "http://server.spring.zhang.pers/") +@WebService(name = IMasterDataSyncService.SERVICE_NAME, targetNamespace = IMasterDataSyncService.TARGET_NAMESPACE) public interface IMasterDataSyncService { + String SERVICE_NAME = "MasterDataSyncService"; + + String TARGET_NAMESPACE = "http://server.spring.zhang.pers/"; + /** * 同步物料数据 + * * @param materialListStr 物料列表 */ @WebMethod(operationName = "syncMaterial") - String syncMaterial(@WebParam(name = "materialList") Spring materialListStr); + @WebResult + String syncMaterial(@WebParam(name = "materialListStr") String materialListStr); /** * 同步物料分类数据 + * * @param materialCategoryListStr 物料分类列表 */ @WebMethod(operationName = "syncMaterialCategory") - String syncMaterialCategory(@WebParam(name = "materialCategoryList") Spring materialCategoryListStr); + @WebResult + String syncMaterialCategory(@WebParam(name = "materialCategoryList") String materialCategoryListStr); /** * 同步计量单位数据 + * * @param unitListStr 计量单位列表 */ @WebMethod(operationName = "syncUnit") - String syncUnit(@WebParam(name = "unitList") Spring unitListStr); + @WebResult + String syncUnit(@WebParam(name = "unitList") String unitListStr); } diff --git a/ktg-mes/src/main/java/com/ktg/mes/md/service/impl/MasterDataSyncServiceImpl.java b/ktg-mes/src/main/java/com/ktg/mes/md/service/impl/MasterDataSyncServiceImpl.java index ef3ad70..a8a0bb9 100644 --- a/ktg-mes/src/main/java/com/ktg/mes/md/service/impl/MasterDataSyncServiceImpl.java +++ b/ktg-mes/src/main/java/com/ktg/mes/md/service/impl/MasterDataSyncServiceImpl.java @@ -9,29 +9,40 @@ import com.ktg.mes.md.service.IMdUnitMeasureService; import lombok.RequiredArgsConstructor; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.map.ObjectMapper; +import org.springframework.stereotype.Service; +import javax.jws.WebService; import javax.swing.*; import java.util.List; -import java.util.Objects; @RequiredArgsConstructor +@Service +@WebService(name = IMasterDataSyncService.SERVICE_NAME, targetNamespace = IMasterDataSyncService.TARGET_NAMESPACE, endpointInterface = "com.ktg.mes.md.service.IMasterDataSyncService") public class MasterDataSyncServiceImpl implements IMasterDataSyncService { private final ItemTypeServiceImpl itemTypeService; private final IMdItemService mdItemService; private final IMdUnitMeasureService mdUnitMeasureService; + // 无参构造函数(供 CXF 使用) + public MasterDataSyncServiceImpl() { + this.itemTypeService = null; + this.mdItemService = null; + this.mdUnitMeasureService = null; + } + /** * 同步物料数据 + * * @param materialListStr 物料列表 */ @Override - public String syncMaterial(Spring materialListStr) { + public String syncMaterial(String materialListStr) { // 使用 ObjectMapper 来处理 JSON ObjectMapper objectMapper = new ObjectMapper(); try { // 解析传入的 JSON 字符串 - String jsonList = objectMapper.readTree(materialListStr.toString()).get("LIST").asText(); + String jsonList = objectMapper.readTree(materialListStr).get("LIST").asText(); JsonNode jsonNode = objectMapper.readTree(jsonList); for (JsonNode node : jsonNode) { @@ -77,16 +88,17 @@ public class MasterDataSyncServiceImpl implements IMasterDataSyncService { /** * 同步物料分类数据 + * * @param materialCategoryListStr 物料分类列表 */ @Override - public String syncMaterialCategory(Spring materialCategoryListStr) { + public String syncMaterialCategory(String materialCategoryListStr) { // 使用 ObjectMapper 来处理 JSON ObjectMapper objectMapper = new ObjectMapper(); try { // 解析传入的 JSON 字符串 - String jsonList = objectMapper.readTree(materialCategoryListStr.toString()).get("LIST").asText(); + String jsonList = objectMapper.readTree(materialCategoryListStr).get("LIST").asText(); JsonNode jsonNode = objectMapper.readTree(jsonList); for (JsonNode node : jsonNode) { @@ -144,16 +156,17 @@ public class MasterDataSyncServiceImpl implements IMasterDataSyncService { /** * 同步计量单位数据 + * * @param unitListStr 计量单位列表 */ @Override - public String syncUnit(Spring unitListStr) { + public String syncUnit(String unitListStr) { // 使用 ObjectMapper 来处理 JSON ObjectMapper objectMapper = new ObjectMapper(); try { // 解析传入的 JSON 字符串 - String jsonList = objectMapper.readTree(unitListStr.toString()).get("LIST").asText(); + String jsonList = objectMapper.readTree(unitListStr).get("LIST").asText(); JsonNode jsonNode = objectMapper.readTree(jsonList); for (JsonNode node : jsonNode) {