内置http执行器增加自定义响应结果判断

This commit is contained in:
邵立佳 2025-04-21 15:44:25 +08:00
parent 39e44bf2ae
commit fbf6998762
2 changed files with 11 additions and 9 deletions

View File

@ -73,7 +73,7 @@ public class SnailJobProperties {
/**
* 内置http执行器自定义响应结果配置
*/
private HttpResponse httpResponse = new HttpResponse();
private HttpResponse httpResponse;
/**
* 重试模块配置
@ -119,17 +119,17 @@ public class SnailJobProperties {
/**
* 内置http执行器响应成功状态码默认值200
*/
private int code;
private int code = 200;
/**
* 内置http执行器状态码字段名称默认值code只针对responseType等于json生效
*/
private String field;
private String field = "code";
/**
* 内置http执行器响应类型可选值json或者text默认值json
*/
private String responseType;
private String responseType = "json";
}
@Data

View File

@ -30,6 +30,8 @@ public abstract class AbstractHttpExecutor {
private static final String HTTP_PREFIX = "http://";
private static final int HTTP_SUCCESS_CODE = 200;
private static final Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
private static final String JSON_RESPONSE_TYPE = "json";
private static final String TEXT_RESPONSE_TYPE = "text";
public ExecuteResult process(HttpParams httpParams) {
if (httpParams == null) {
@ -88,13 +90,13 @@ public abstract class AbstractHttpExecutor {
}
// 如果配置了httpResponse则根据响应类型进行进一步验证
if (Objects.nonNull(httpResponse)) {
int code = Optional.of(httpResponse.getCode()).orElse(HTTP_SUCCESS_CODE);
String field = Optional.of(httpResponse.getField()).orElse("code");
String responseType = Optional.of(httpResponse.getResponseType()).orElse("json");
int code = httpResponse.getCode();
String field = httpResponse.getField();
String responseType = httpResponse.getResponseType();
// 根据不同的响应类型进行验证
if ("json".equalsIgnoreCase(responseType)) {
if (JSON_RESPONSE_TYPE.equalsIgnoreCase(responseType)) {
return validateJsonResponse(body, code, field, httpRequest);
} else if ("text".equalsIgnoreCase(responseType)) {
} else if (TEXT_RESPONSE_TYPE.equalsIgnoreCase(responseType)) {
return validateTextResponse(body, code, httpRequest);
} else {
return ExecuteResult.failure("the responseType is not json or text");