205 lines
7.5 KiB
Java
205 lines
7.5 KiB
Java
![]() |
package com.example.processor;
|
|||
|
|
|||
|
import com.example.model.AdvancedSearchRequest;
|
|||
|
import com.example.model.PageRequest;
|
|||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
|
import org.apache.camel.Exchange;
|
|||
|
import org.apache.camel.Configuration;
|
|||
|
import org.apache.camel.BindToRegistry;
|
|||
|
|
|||
|
import org.slf4j.Logger;
|
|||
|
import org.slf4j.LoggerFactory;
|
|||
|
|
|||
|
import java.util.Date;
|
|||
|
import java.util.HashMap;
|
|||
|
import java.util.Map;
|
|||
|
|
|||
|
/**
|
|||
|
* 统一请求处理器
|
|||
|
* 适用于Camel Karavan低代码平台配置
|
|||
|
* 通用处理器,不依赖于特定实体类型
|
|||
|
*/
|
|||
|
@Configuration
|
|||
|
@BindToRegistry("requestProcessor")
|
|||
|
public class RequestProcessor {
|
|||
|
private static final Logger LOGGER = LoggerFactory.getLogger(RequestProcessor.class);
|
|||
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
|||
|
|
|||
|
/**
|
|||
|
* 从请求头中提取ID参数
|
|||
|
*/
|
|||
|
public Long extractId(Exchange exchange) {
|
|||
|
String idStr = exchange.getIn().getHeader("id", String.class);
|
|||
|
return Long.parseLong(idStr);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 从请求体解析分页参数
|
|||
|
*/
|
|||
|
public Map<String, Object> extractPageParams(Exchange exchange) throws Exception {
|
|||
|
String body = exchange.getIn().getBody(String.class);
|
|||
|
PageRequest pageRequest = objectMapper.readValue(body, PageRequest.class);
|
|||
|
|
|||
|
// 保存请求对象供后续使用
|
|||
|
exchange.setProperty("pageRequest", pageRequest);
|
|||
|
|
|||
|
Map<String, Object> params = new HashMap<>();
|
|||
|
params.put("offset", pageRequest.getOffset());
|
|||
|
params.put("limit", pageRequest.getLimit());
|
|||
|
params.put("filters", pageRequest.getFilters());
|
|||
|
params.put("sortField", pageRequest.getSortField());
|
|||
|
params.put("sortOrder", pageRequest.getSortOrder());
|
|||
|
|
|||
|
return params;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 从请求体解析高级搜索参数
|
|||
|
*/
|
|||
|
public Map<String, Object> extractSearchParams(Exchange exchange) throws Exception {
|
|||
|
String body = exchange.getIn().getBody(String.class);
|
|||
|
AdvancedSearchRequest searchRequest = objectMapper.readValue(body, AdvancedSearchRequest.class);
|
|||
|
|
|||
|
// 保存请求对象供后续使用
|
|||
|
exchange.setProperty("searchRequest", searchRequest);
|
|||
|
|
|||
|
Map<String, Object> params = new HashMap<>();
|
|||
|
params.put("offset", searchRequest.getOffset());
|
|||
|
params.put("limit", searchRequest.getLimit());
|
|||
|
params.put("criteria", searchRequest.getCriteria());
|
|||
|
params.put("sortField", searchRequest.getSortField());
|
|||
|
params.put("sortOrder", searchRequest.getSortOrder());
|
|||
|
|
|||
|
return params;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 准备实体创建参数,通用方法
|
|||
|
* @param <T> 实体类型
|
|||
|
* @param exchange Camel交换对象
|
|||
|
* @param entityClass 实体类Class
|
|||
|
* @return 准备好的实体对象
|
|||
|
*/
|
|||
|
public <T> T prepareEntity(Exchange exchange, Class<T> entityClass) throws Exception {
|
|||
|
String body = exchange.getIn().getBody(String.class);
|
|||
|
T entity = objectMapper.readValue(body, entityClass);
|
|||
|
|
|||
|
// 通过反射设置创建和更新时间(如果实体有这些字段)
|
|||
|
try {
|
|||
|
// 尝试设置创建时间
|
|||
|
try {
|
|||
|
java.lang.reflect.Method setCreatedAt = entity.getClass().getMethod("setCreatedAt", Date.class);
|
|||
|
if (setCreatedAt != null) {
|
|||
|
setCreatedAt.invoke(entity, new Date());
|
|||
|
}
|
|||
|
} catch (NoSuchMethodException e) {
|
|||
|
// 实体没有这个方法,忽略
|
|||
|
}
|
|||
|
|
|||
|
// 尝试设置更新时间
|
|||
|
try {
|
|||
|
java.lang.reflect.Method setUpdatedAt = entity.getClass().getMethod("setUpdatedAt", Date.class);
|
|||
|
if (setUpdatedAt != null) {
|
|||
|
setUpdatedAt.invoke(entity, new Date());
|
|||
|
}
|
|||
|
} catch (NoSuchMethodException e) {
|
|||
|
// 实体没有这个方法,忽略
|
|||
|
}
|
|||
|
|
|||
|
// 尝试设置默认激活状态
|
|||
|
try {
|
|||
|
java.lang.reflect.Method getActive = entity.getClass().getMethod("getActive");
|
|||
|
java.lang.reflect.Method setActive = entity.getClass().getMethod("setActive", Boolean.class);
|
|||
|
|
|||
|
if (getActive != null && setActive != null) {
|
|||
|
Boolean active = (Boolean) getActive.invoke(entity);
|
|||
|
if (active == null) {
|
|||
|
setActive.invoke(entity, true);
|
|||
|
}
|
|||
|
}
|
|||
|
} catch (NoSuchMethodException e) {
|
|||
|
// 实体没有这些方法,忽略
|
|||
|
}
|
|||
|
} catch (Exception e) {
|
|||
|
LOGGER.warn("设置实体属性时出错", e);
|
|||
|
}
|
|||
|
|
|||
|
return entity;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 准备实体更新参数,通用方法
|
|||
|
* @param <T> 实体类型
|
|||
|
* @param exchange Camel交换对象
|
|||
|
* @param entityClass 实体类Class
|
|||
|
* @return 准备好的实体对象
|
|||
|
*/
|
|||
|
public <T> T prepareEntityWithId(Exchange exchange, Class<T> entityClass) throws Exception {
|
|||
|
String idStr = exchange.getIn().getHeader("id", String.class);
|
|||
|
Long id = Long.parseLong(idStr);
|
|||
|
|
|||
|
String body = exchange.getIn().getBody(String.class);
|
|||
|
T entity = objectMapper.readValue(body, entityClass);
|
|||
|
|
|||
|
// 通过反射设置ID和更新时间
|
|||
|
try {
|
|||
|
// 设置ID
|
|||
|
try {
|
|||
|
java.lang.reflect.Method setId = entity.getClass().getMethod("setId", Long.class);
|
|||
|
if (setId != null) {
|
|||
|
setId.invoke(entity, id);
|
|||
|
}
|
|||
|
} catch (NoSuchMethodException e) {
|
|||
|
LOGGER.warn("实体类 {} 没有 setId 方法", entityClass.getName());
|
|||
|
}
|
|||
|
|
|||
|
// 设置更新时间
|
|||
|
try {
|
|||
|
java.lang.reflect.Method setUpdatedAt = entity.getClass().getMethod("setUpdatedAt", Date.class);
|
|||
|
if (setUpdatedAt != null) {
|
|||
|
setUpdatedAt.invoke(entity, new Date());
|
|||
|
}
|
|||
|
} catch (NoSuchMethodException e) {
|
|||
|
// 实体没有这个方法,忽略
|
|||
|
}
|
|||
|
} catch (Exception e) {
|
|||
|
LOGGER.warn("设置实体属性时出错", e);
|
|||
|
}
|
|||
|
|
|||
|
return entity;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 创建实体不存在的错误响应
|
|||
|
*/
|
|||
|
public Map<String, String> createNotFoundResponse() {
|
|||
|
Map<String, String> response = new HashMap<>();
|
|||
|
response.put("error", "记录不存在");
|
|||
|
return response;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 创建不存在的错误响应(指定实体名称)
|
|||
|
*/
|
|||
|
public Map<String, String> createNotFoundResponse(String entityName) {
|
|||
|
Map<String, String> response = new HashMap<>();
|
|||
|
response.put("error", entityName + "不存在");
|
|||
|
return response;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 创建冲突的错误响应
|
|||
|
*/
|
|||
|
public Map<String, String> createConflictResponse(String field) {
|
|||
|
Map<String, String> response = new HashMap<>();
|
|||
|
response.put("error", field + "已存在");
|
|||
|
return response;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 设置HTTP状态码
|
|||
|
*/
|
|||
|
public void setStatusCode(Exchange exchange, int code) {
|
|||
|
exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, code);
|
|||
|
}
|
|||
|
}
|