diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 6c94aec44c..b0c36c399a 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -282,6 +282,8 @@ public class ApiDefinitionService { request.setMethod("dubbo://"); } ApiDefinitionWithBLOBs returnModel = updateTest(request); + MockConfigService mockConfigService = CommonBeanFactory.getBean(MockConfigService.class); + mockConfigService.updateMockReturnMsgByApi(returnModel); FileUtils.createBodyFiles(request.getRequest().getId(), bodyFiles); return returnModel; } diff --git a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java index bbf42009e8..e75e33fe02 100644 --- a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java +++ b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java @@ -27,6 +27,7 @@ import io.metersphere.commons.utils.*; import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.i18n.Translator; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.SqlSession; import org.springframework.stereotype.Service; @@ -1332,4 +1333,33 @@ public class MockConfigService { } } + + public void updateMockReturnMsgByApi(ApiDefinitionWithBLOBs apiDefinitionWithBLOBs) { + if(apiDefinitionWithBLOBs == null){ + return; + } + Map returnMap = MockApiUtils.getApiResponse(apiDefinitionWithBLOBs.getResponse()); + if(MapUtils.isEmpty(returnMap) || !returnMap.containsKey("returnMsg")){ + return; + } + List updateList = this.selectMockExpectConfigByApiId(apiDefinitionWithBLOBs.getId()); + if(CollectionUtils.isNotEmpty(updateList)){ + for (MockExpectConfigWithBLOBs model: updateList) { + if(StringUtils.isNotEmpty(model.getResponse())){ + try { + JSONObject responseObj = JSONObject.parseObject(model.getResponse()); + if(responseObj.containsKey("responseResult")){ + JSONObject responseResultObject = responseObj.getJSONObject("responseResult"); + if(responseResultObject.containsKey("body")){ + responseResultObject.getJSONObject("body").put("apiRspRaw",returnMap.get("returnMsg")); + + model.setResponse(responseObj.toJSONString()); + mockExpectConfigMapper.updateByPrimaryKeySelective(model); + } + } + }catch (Exception e){} + } + } + } + } }