如何记录具有@JsonIgnore注释的字段



我将Spring Boot 2.7.3Java 17&Gradle。我需要在日志文件中记录每个DB操作。除了一种情况外,一切都很好。当我在UI类中使用@JsonIgnore时,不会记录这些特定字段。其他字段已正确记录。如何处理?

我的特性类:

@Aspect
@Component
public class LoggingAspect {
private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);
@Pointcut("execution(public * org.mycomp.erp.service..*(..))")
public void requestPointcut() {}
@Before("requestPointcut()")
public void requestLogger(JoinPoint joinPoint) throws JsonProcessingException {
final ObjectMapper objectMapper = new ObjectMapper();
final String methodName = joinPoint.getSignature().getName();
final Object[] inputArguments = joinPoint.getArgs();
final String logMessage = "Operation : " + methodName +
" With Input Parameters : " + objectMapper.writeValueAsString(inputArguments);
logger.info(logMessage);
}
}

我的UI记录类:

public record LeaveApplUx(
@JsonIgnore String employeeCode,
@JsonIgnore int applicationNumber,
LocalDate startDate,
LocalDate endDate,
String leaveType) {
public LeaveApplUx(
@Size(min = 4, max = 5, message = "Employee Code Must Be Within 4 To 5 Character Long Or Blank")
@JsonProperty("employeeCode") String employeeCode,
@Range(min = 0, max = 9999, message = "Application Number Must Be Within 9999 Or Zero")
@JsonProperty("applicationNumber") int applicationNumber,
@NotNull(message = "Start Date Empty")
@JsonSerialize(using = LocalDateSerializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
@JsonProperty("startDate") LocalDate startDate,
@NotNull(message = "End Date Empty")
@JsonSerialize(using = LocalDateSerializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
@JsonProperty("endDate") LocalDate endDate,
@Size(min = 2, max = 2, message = "Leave Type Mis-Match")
@JsonProperty("leaveType") String leaveType) {
this.employeeCode = employeeCode;
this.applicationNumber = applicationNumber;
this.startDate = startDate;
this.endDate = endDate;
this.leaveType = leaveType;
}
}

我通过邮差的请求:

DELETE
http://localhost:8080/hr/leave/application/delete
{
"employeeCode": "E001",
"applicationNumber": "1",
"startDate": "01-04-2022",
"endDate": "05-04-2022",
"leaveType": "CL"
}

我的日志文件输出:

2022-08-30 14:32:53,688 INFO  :  Operation : deleteLeaveApplication With Input Parameters : ["N","N",{"startDate":"01-04-2022","endDate":"05-04-2022","leaveType":"CL"}]

员工守则;未记录应用程序编号。

注释@JsonIgnore导致Jackson的ObjectMapper在序列化时忽略这些字段。这是已定义的行为。

标记注释,指示访问器的逻辑属性(字段、getter/setter方法或[JsonCreator注释的构造函数或工厂方法]的Creator参数(将被基于内省的序列化和反序列化功能忽略。

您有两个选项:将日志记录为数组或为日志记录配置ObjectMapper。


解决方案#1:作为数组登录

您有所有Object[] inputArguments阵列中可用的参数,这些参数可以直接记录。我还建议使用适当的日志记录功能:

logger.info("Operation: {} with input parameters: {}", 
methodName, inputArguments);

解决方案#2:配置特定的ObjectMapper

对于这种特殊情况,可以通过将MapperFeature.USE_ANNOTATIONS设置为false,将ObjectMapper配置为忽略注释。

确定是否将注释内省用于配置的功能;如果启用,将使用配置的AnnotationInspector:如果禁用,则不考虑任何注释。

基于Jackson版本:

  • 2.13之前的杰克逊:
    ObjectMapper ignoringObjectMapper = new ObjectMapper() 
    .configure(MapperFeature.USE_ANNOTATIONS, false);
    
  • Jackson 2.13及更高版本:
    ObjectMapper ignoringObjectMapper = JsonMapper.builder()
    .configure(MapperFeature.USE_ANNOTATIONS, false)
    .build();
    

。。。并记录:

logger.info("Operation: {} with input parameters: {}", 
methodName, ignoringObjectMapper.writeValueAsString(inputArguments));

问题解决了。只需更换

objectMapper.writeValueAsString(inputArguments)

用这个

Arrays.toString(inputArguments)

相关内容

  • 没有找到相关文章

最新更新