2025/3/15 19:07:52---将json修改为fastjson

This commit is contained in:
2025-03-15 11:08:17 +00:00 committed by root
parent 2a12d523df
commit df50faaeab
2 changed files with 36 additions and 26 deletions

View File

@ -2,12 +2,12 @@ package com.example.processor;
import com.example.model.AdvancedSearchRequest; import com.example.model.AdvancedSearchRequest;
import com.example.model.PageRequest; import com.example.model.PageRequest;
import com.fasterxml.jackson.databind.ObjectMapper; import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.alibaba.fastjson.JSONObject;
import org.apache.camel.Exchange; import com.alibaba.fastjson.serializer.SerializeConfig;
import org.apache.camel.Configuration; import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.camel.BindToRegistry;
import org.apache.camel.Exchange;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -20,11 +20,18 @@ import java.util.Map;
* 适用于Camel Karavan低代码平台配置 * 适用于Camel Karavan低代码平台配置
* 通用处理器不依赖于特定实体类型 * 通用处理器不依赖于特定实体类型
*/ */
@Configuration
@BindToRegistry("requestProcessor")
public class RequestProcessor { public class RequestProcessor {
private static final Logger LOGGER = LoggerFactory.getLogger(RequestProcessor.class); private static final Logger LOGGER = LoggerFactory.getLogger(RequestProcessor.class);
private final ObjectMapper objectMapper = new ObjectMapper(); private final SerializeConfig serializeConfig;
public RequestProcessor() {
// 配置Fastjson序列化
serializeConfig = new SerializeConfig();
// 配置日期序列化为时间戳
serializeConfig.put(Date.class, (serializer, object, fieldName, fieldType, features) -> {
serializer.write(((Date) object).getTime());
});
}
/** /**
* 从请求头中提取ID参数 * 从请求头中提取ID参数
@ -34,7 +41,7 @@ public class RequestProcessor {
return Long.parseLong(idStr); return Long.parseLong(idStr);
} }
/** /**
* 从请求体解析分页参数 * 从请求体解析分页参数
*/ */
public Map<String, Object> extractPageCountParams(Exchange exchange) throws Exception { public Map<String, Object> extractPageCountParams(Exchange exchange) throws Exception {
@ -43,7 +50,7 @@ public class RequestProcessor {
if(body instanceof String){ if(body instanceof String){
String bodyStr = (String) body; String bodyStr = (String) body;
LOGGER.info("分页拿到的请求body: {}", bodyStr); LOGGER.info("分页拿到的请求body: {}", bodyStr);
pageRequest = objectMapper.readValue(bodyStr, PageRequest.class); pageRequest = JSON.parseObject(bodyStr, PageRequest.class);
// 保存请求对象供后续使用 // 保存请求对象供后续使用
exchange.setProperty("pageRequest", pageRequest); exchange.setProperty("pageRequest", pageRequest);
@ -51,7 +58,7 @@ public class RequestProcessor {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> bodyMap = (Map<String, Object>) body; Map<String, Object> bodyMap = (Map<String, Object>) body;
LOGGER.info("分页拿到的请求body: {}", bodyMap); LOGGER.info("分页拿到的请求body: {}", bodyMap);
pageRequest = objectMapper.convertValue(bodyMap, PageRequest.class); pageRequest = JSONObject.toJavaObject(new JSONObject(bodyMap), PageRequest.class);
}else{ }else{
// 处理其他类型或null // 处理其他类型或null
LOGGER.warn("无法处理的请求体类型: {}", body != null ? body.getClass().getName() : "null"); LOGGER.warn("无法处理的请求体类型: {}", body != null ? body.getClass().getName() : "null");
@ -94,29 +101,28 @@ public class RequestProcessor {
AdvancedSearchRequest searchRequest = null; AdvancedSearchRequest searchRequest = null;
if(body instanceof String){ if(body instanceof String){
String bodyStr = (String) body; String bodyStr = (String) body;
LOGGER.info("高级搜索拿到的请求body--string: {}", bodyStr); LOGGER.info("高级搜索拿到的请求body: {}", bodyStr);
searchRequest = objectMapper.readValue(bodyStr, AdvancedSearchRequest.class); searchRequest = JSON.parseObject(bodyStr, AdvancedSearchRequest.class);
}else if(body instanceof Map){ }else if(body instanceof Map){
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> bodyMap = (Map<String, Object>) body; Map<String, Object> bodyMap = (Map<String, Object>) body;
LOGGER.info("高级搜索拿到的请求body---map: {}", bodyMap); LOGGER.info("高级搜索拿到的请求body: {}", bodyMap);
searchRequest = objectMapper.convertValue(bodyMap, AdvancedSearchRequest.class); searchRequest = JSONObject.toJavaObject(new JSONObject(bodyMap), AdvancedSearchRequest.class);
}else if(body instanceof JSONObject){
searchRequest = JSONObject.toJavaObject((JSONObject)body, AdvancedSearchRequest.class);
}else{ }else{
LOGGER.warn("无法处理的请求体类型: {}---body的类型{}", body != null ? body.getClass().getName() : "null",body.getClass()); LOGGER.warn("无法处理的请求体类型: {}", body != null ? body.getClass().getName() : "null");
searchRequest = new AdvancedSearchRequest(); searchRequest = new AdvancedSearchRequest();
} }
// 保存请求对象供后续使用 // 保存请求对象供后续使用
exchange.setProperty("searchRequest", searchRequest); exchange.setProperty("searchRequest", searchRequest);
Map<String, Object> params = new HashMap<>(); // 使用日期序列化为时间戳的配置序列化对象
params.put("offset", searchRequest.getOffset()); String searchRequestStr = JSON.toJSONString(searchRequest, serializeConfig, SerializerFeature.WriteMapNullValue);
params.put("limit", searchRequest.getLimit()); @SuppressWarnings("unchecked")
params.put("criteria", searchRequest.getCriteria()); Map<String, Object> params = JSON.parseObject(searchRequestStr, Map.class);
params.put("sortField", searchRequest.getSortField());
params.put("sortOrder", searchRequest.getSortOrder());
LOGGER.info("转换后参数:{}",params);
return params; return params;
} }
@ -147,7 +153,7 @@ public class RequestProcessor {
*/ */
public <T> T prepareEntity(Exchange exchange, Class<T> entityClass) throws Exception { public <T> T prepareEntity(Exchange exchange, Class<T> entityClass) throws Exception {
String body = exchange.getIn().getBody(String.class); String body = exchange.getIn().getBody(String.class);
T entity = objectMapper.readValue(body, entityClass); T entity = JSON.parseObject(body, entityClass);
// 通过反射设置创建和更新时间如果实体有这些字段 // 通过反射设置创建和更新时间如果实体有这些字段
try { try {
@ -204,7 +210,7 @@ public class RequestProcessor {
Long id = Long.parseLong(idStr); Long id = Long.parseLong(idStr);
String body = exchange.getIn().getBody(String.class); String body = exchange.getIn().getBody(String.class);
T entity = objectMapper.readValue(body, entityClass); T entity = JSON.parseObject(body, entityClass);
// 通过反射设置ID和更新时间 // 通过反射设置ID和更新时间
try { try {

View File

@ -0,0 +1,4 @@
# 添加私有仓库
maven.repos.myrepo=http://192.168.56.12:9081/repository/gzkx-maven-public/
deps=\
com.alibaba:fastjson:2.0.56