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

This commit is contained in:
邵立佳 2025-04-21 17:24:31 +08:00
parent fbf6998762
commit 3ac7a5b312
2 changed files with 11 additions and 5 deletions

View File

@ -119,7 +119,7 @@ public class SnailJobProperties {
/**
* 内置http执行器响应成功状态码默认值200
*/
private int code = 200;
private Integer code = 200;
/**
* 内置http执行器状态码字段名称默认值code只针对responseType等于json生效

View File

@ -30,9 +30,10 @@ 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 int RESPONSE_SUCCESS_CODE = 200;
private static final String RESPONSE_CODE_FIELD = "code";
private static final String JSON_RESPONSE_TYPE = "json";
private static final String TEXT_RESPONSE_TYPE = "text";
public ExecuteResult process(HttpParams httpParams) {
if (httpParams == null) {
String message = "HttpParams is null. Verify jobParam configuration.";
@ -90,9 +91,14 @@ public abstract class AbstractHttpExecutor {
}
// 如果配置了httpResponse则根据响应类型进行进一步验证
if (Objects.nonNull(httpResponse)) {
int code = httpResponse.getCode();
String field = httpResponse.getField();
String responseType = httpResponse.getResponseType();
// 防止显示声明字段但是未配置值
int code = Optional.ofNullable(httpResponse.getCode()).orElse(RESPONSE_SUCCESS_CODE);
String field = Optional.ofNullable(httpResponse.getField())
.filter(StringUtils::hasLength)
.orElse(RESPONSE_CODE_FIELD);
String responseType = Optional.ofNullable(httpResponse.getField())
.filter(StringUtils::hasLength)
.orElse(JSON_RESPONSE_TYPE);
// 根据不同的响应类型进行验证
if (JSON_RESPONSE_TYPE.equalsIgnoreCase(responseType)) {
return validateJsonResponse(body, code, field, httpRequest);