jackson 如何记录带有@JsonIgnore注解的字段?

fhg3lkii  于 2022-11-08  发布在  其他
关注(0)|答案(2)|浏览(131)

我正在使用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;

    }
}

我通过 Postman 提出的要求:
第一个
我的日志文件输出:

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"}]

未记录员工代码和申请编号。

5hcedyr0

5hcedyr01#

注解@JsonIgnore导致Jackson的ObjectMapper在序列化时忽略这些字段。
标记注解,指出逻辑属性,存取子(字段、getter/setter方法或Creator参数[属于JsonCreator-注解的建构函式或作坊方法])将由以内省为基础的序列化和还原序列化功能忽略。
您有两个选择:记录为数组或配置ObjectMapper进行日志记录。

解决方案1:记录为数组

您在Object[] inputArguments数组中有 * 所有 * 可以直接记录的参数。

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

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

对于这种特殊情况,您可以通过将MapperFeature.USE_ANNOTATIONS设置为false来配置ObjectMapper以忽略注解。
用于确定是否将注解自检用于配置的功能;如果启用,将使用已配置的AnnotationIntrospector:如果禁用,则不考虑注解。
根据Jackson的版本:

  • Jackson2.13:
ObjectMapper ignoringObjectMapper = new ObjectMapper() 
    .configure(MapperFeature.USE_ANNOTATIONS, false);
  • Jackson2.13及更高版本:
ObjectMapper ignoringObjectMapper = JsonMapper.builder()
        .configure(MapperFeature.USE_ANNOTATIONS, false)
        .build();

...并记录:

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

e4yzc0pl2#

问题解决了,更换即可

objectMapper.writeValueAsString(inputArguments)

用这个

Arrays.toString(inputArguments)

相关问题