diff --git a/sf-vertx/src/main/java/com/sf/vertx/handle/ApiMockHandlerImpl.java b/sf-vertx/src/main/java/com/sf/vertx/handle/ApiMockHandlerImpl.java index e91a29b..0cd50ca 100644 --- a/sf-vertx/src/main/java/com/sf/vertx/handle/ApiMockHandlerImpl.java +++ b/sf-vertx/src/main/java/com/sf/vertx/handle/ApiMockHandlerImpl.java @@ -1,31 +1,29 @@ package com.sf.vertx.handle; -import com.sf.vertx.api.pojo.AppConfig; import com.sf.vertx.api.pojo.MockResponse; import com.sf.vertx.constans.SacErrorCode; import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.handler.HttpException; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; @Slf4j public class ApiMockHandlerImpl implements ApiMockHandler { - @Override - public void handle(RoutingContext rc) { - try { - String cacheKey = AppConfigHandler.getApiCodeConfigCacheKey(rc); - // mock - MockResponse mockResponse = AppConfigHandler.mock(cacheKey,rc); - if (mockResponse != null) { - rc.fail(new MockException(mockResponse.getHttpStatus(), mockResponse.getMockResponse())); - return; - } - } catch (Exception e) { - e.printStackTrace(); - rc.fail(new HttpException(SacErrorCode.DEFAULT_ERROR_CODE)); - return; - } - rc.next(); - } + @Override + public void handle(RoutingContext rc) { + try { + String cacheKey = AppConfigHandler.getApiCodeConfigCacheKey(rc); + // mock + MockResponse mockResponse = AppConfigHandler.mock(cacheKey, rc); + if (mockResponse != null) { + rc.fail(new MockException(mockResponse.getHttpStatus(), mockResponse.getMockResponse())); + return; + } + } catch (Exception e) { + e.printStackTrace(); + rc.fail(new HttpException(SacErrorCode.DEFAULT_ERROR_CODE)); + return; + } + rc.next(); + } } diff --git a/sf-vertx/src/main/java/com/sf/vertx/handle/AppConfigHandler.java b/sf-vertx/src/main/java/com/sf/vertx/handle/AppConfigHandler.java index 58226fe..f6dd674 100644 --- a/sf-vertx/src/main/java/com/sf/vertx/handle/AppConfigHandler.java +++ b/sf-vertx/src/main/java/com/sf/vertx/handle/AppConfigHandler.java @@ -257,6 +257,7 @@ public class AppConfigHandler { public static MockResponse mock(String key, RoutingContext rc) { if (APICODE_CONFIG_MAP.get(key) != null && APICODE_CONFIG_MAP.get(key).getMockDefaultHttpStatus() != null){ + // 如果是sac服务,query和body参数都从body中取 Integer httpStatus = APICODE_CONFIG_MAP.get(key).getMockDefaultHttpStatus(); String mockResponse = APICODE_CONFIG_MAP.get(key).getMockDefaultResponse(); List mockExpectations = APICODE_CONFIG_MAP.get(key).getMockExpectations(); @@ -278,7 +279,7 @@ public class AppConfigHandler { String parameterPosition = matchCondition.getParameterPosition(); String parameterKey = matchCondition.getParameterKey(); List parameterValue = matchCondition.getParameterValue(); - String requestParameterValue = getRequestParameterValue(parameterPosition,parameterKey,rc.request(),jsonBody); + String requestParameterValue = getRequestParameterValue(AppConfigHandler.isServiceTypeSac(key),parameterPosition,parameterKey,rc.request(),jsonBody); if (!MatchType.IS_NULL.equals(matchType) && !MatchType.NOT_NULL.equals(matchType)){ // 需要值而没有设置值,直接匹配失败 if (CollectionUtil.isEmpty(parameterValue) || requestParameterValue == null) { @@ -352,9 +353,12 @@ public class AppConfigHandler { return null; } - private static String getRequestParameterValue(String parameterPosition, String parameterKey, HttpServerRequest request,JsonObject jsonBody) { + private static String getRequestParameterValue(Boolean isServiceTypeSac,String parameterPosition, String parameterKey, HttpServerRequest request,JsonObject jsonBody) { switch (parameterPosition){ case "query": + if (isServiceTypeSac){ + return jsonBody.getString(parameterKey); + } return request.getParam(parameterKey); case "header": return request.getHeader(parameterKey); diff --git a/sf-vertx/src/main/java/com/sf/vertx/handle/RestfulFailureHandlerImpl.java b/sf-vertx/src/main/java/com/sf/vertx/handle/RestfulFailureHandlerImpl.java index 4dfc21c..d6cd4a7 100644 --- a/sf-vertx/src/main/java/com/sf/vertx/handle/RestfulFailureHandlerImpl.java +++ b/sf-vertx/src/main/java/com/sf/vertx/handle/RestfulFailureHandlerImpl.java @@ -1,5 +1,6 @@ package com.sf.vertx.handle; +import io.vertx.core.buffer.Buffer; import org.apache.commons.lang3.StringUtils; import com.sf.vertx.constans.SacErrorCode; @@ -20,7 +21,7 @@ public class RestfulFailureHandlerImpl implements RestfulFailureHandler { Integer sc = SacErrorCode.DEFAULT_ERROR_CODE; try { Throwable failure = frc.failure(); - log.info("failure:{}", failure); + log.info("failure:", failure); if (failure instanceof HttpException) { HttpException httpException = (HttpException) failure; sc = httpException.getStatusCode(); @@ -49,7 +50,7 @@ public class RestfulFailureHandlerImpl implements RestfulFailureHandler { frc.response().setChunked(true).setStatusCode(statusCode).putHeader("Content-Type", "application/json") .putHeader(AppConfigHandler.sacResponseHeaderKey(), String.valueOf(sc)) - .putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(errorJson.size())).end(errorJson.toBuffer()); + .end(errorJson.toBuffer()); return; }