Browse Source
- 简化了 JacksonConfig 类,移除了自定义时间格式配置 - 添加了 JacksonConfigChecker组件,用于检查 Jackson 配置是否正确 - 新增 JacksonTestController,用于测试 LocalDateTime 序列pan
4 changed files with 161 additions and 42 deletions
@ -0,0 +1,59 @@ |
|||||
|
package com.gxwebsoft.common.core.config; |
||||
|
|
||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.boot.CommandLineRunner; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.time.LocalDateTime; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
/** |
||||
|
* Jackson配置检查器 |
||||
|
* 在应用启动时检查Jackson配置是否正确 |
||||
|
* |
||||
|
* @author WebSoft |
||||
|
* @since 2025-09-08 |
||||
|
*/ |
||||
|
@Component |
||||
|
public class JacksonConfigChecker implements CommandLineRunner { |
||||
|
|
||||
|
private static final Logger logger = LoggerFactory.getLogger(JacksonConfigChecker.class); |
||||
|
|
||||
|
@Autowired |
||||
|
private ObjectMapper objectMapper; |
||||
|
|
||||
|
@Override |
||||
|
public void run(String... args) throws Exception { |
||||
|
logger.info("=== Jackson配置检查开始 ==="); |
||||
|
|
||||
|
try { |
||||
|
// 检查JavaTimeModule是否注册
|
||||
|
boolean hasJavaTimeModule = objectMapper.getRegisteredModuleIds() |
||||
|
.contains(JavaTimeModule.class.getName()); |
||||
|
logger.info("JavaTimeModule是否注册: {}", hasJavaTimeModule); |
||||
|
|
||||
|
// 测试LocalDateTime序列化
|
||||
|
Map<String, Object> testData = new HashMap<>(); |
||||
|
testData.put("currentTime", LocalDateTime.now()); |
||||
|
testData.put("message", "Jackson配置测试"); |
||||
|
|
||||
|
String json = objectMapper.writeValueAsString(testData); |
||||
|
logger.info("LocalDateTime序列化测试成功: {}", json); |
||||
|
|
||||
|
// 测试反序列化
|
||||
|
Map<String, Object> result = objectMapper.readValue(json, Map.class); |
||||
|
logger.info("反序列化测试成功: {}", result); |
||||
|
|
||||
|
logger.info("=== Jackson配置检查完成 - 配置正常 ==="); |
||||
|
|
||||
|
} catch (Exception e) { |
||||
|
logger.error("=== Jackson配置检查失败 ===", e); |
||||
|
logger.error("请检查Jackson配置是否正确"); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,90 @@ |
|||||
|
package com.gxwebsoft.common.core.controller; |
||||
|
|
||||
|
import com.gxwebsoft.common.core.web.ApiResult; |
||||
|
import com.gxwebsoft.common.core.web.BaseController; |
||||
|
import io.swagger.v3.oas.annotations.Operation; |
||||
|
import io.swagger.v3.oas.annotations.tags.Tag; |
||||
|
import org.springframework.web.bind.annotation.GetMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
import java.time.LocalDateTime; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
/** |
||||
|
* Jackson序列化测试控制器 |
||||
|
* 用于测试LocalDateTime序列化是否正常工作 |
||||
|
* |
||||
|
* @author WebSoft |
||||
|
* @since 2025-09-08 |
||||
|
*/ |
||||
|
@Tag(name = "Jackson测试") |
||||
|
@RestController |
||||
|
@RequestMapping("/api/test/jackson") |
||||
|
public class JacksonTestController extends BaseController { |
||||
|
|
||||
|
@Operation(summary = "测试LocalDateTime序列化") |
||||
|
@GetMapping("/datetime") |
||||
|
public ApiResult<Map<String, Object>> testDateTime() { |
||||
|
Map<String, Object> result = new HashMap<>(); |
||||
|
result.put("currentTime", LocalDateTime.now()); |
||||
|
result.put("message", "如果您能看到格式化的时间,说明Jackson配置正常"); |
||||
|
result.put("timestamp", System.currentTimeMillis()); |
||||
|
return success(result); |
||||
|
} |
||||
|
|
||||
|
@Operation(summary = "测试实体类序列化") |
||||
|
@GetMapping("/entity") |
||||
|
public ApiResult<TestEntity> testEntity() { |
||||
|
TestEntity entity = new TestEntity(); |
||||
|
entity.setId(1); |
||||
|
entity.setName("测试实体"); |
||||
|
entity.setCreateTime(LocalDateTime.now()); |
||||
|
entity.setUpdateTime(LocalDateTime.now()); |
||||
|
return success(entity); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 测试实体类 |
||||
|
*/ |
||||
|
public static class TestEntity { |
||||
|
private Integer id; |
||||
|
private String name; |
||||
|
private LocalDateTime createTime; |
||||
|
private LocalDateTime updateTime; |
||||
|
|
||||
|
// Getters and Setters
|
||||
|
public Integer getId() { |
||||
|
return id; |
||||
|
} |
||||
|
|
||||
|
public void setId(Integer id) { |
||||
|
this.id = id; |
||||
|
} |
||||
|
|
||||
|
public String getName() { |
||||
|
return name; |
||||
|
} |
||||
|
|
||||
|
public void setName(String name) { |
||||
|
this.name = name; |
||||
|
} |
||||
|
|
||||
|
public LocalDateTime getCreateTime() { |
||||
|
return createTime; |
||||
|
} |
||||
|
|
||||
|
public void setCreateTime(LocalDateTime createTime) { |
||||
|
this.createTime = createTime; |
||||
|
} |
||||
|
|
||||
|
public LocalDateTime getUpdateTime() { |
||||
|
return updateTime; |
||||
|
} |
||||
|
|
||||
|
public void setUpdateTime(LocalDateTime updateTime) { |
||||
|
this.updateTime = updateTime; |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue