简体   繁体   English

使用 Jackson CsvMapper 来序列化 POJO 排除列(字段)

[英]Exclude colums (fields) using Jackson CsvMapper to serialize POJO

I have a Java class and i want to serialize it into CSV using jackson.我有一个 Java class 并且我想使用 ZB41779690B83F182ACC67DB6AC 将其序列化为 CSV In addition i want to exclude from the csv a single field base on a external property.此外,我想从 csv 中排除基于外部属性的单个字段。

I have tried using all features provides by jackson like Feature.IGNORE_UNKNOWN=true or @JsonIgnoreProperties(ignoreUnknown = true) on my data class, csvMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) but none of them works.我已经尝试使用 jackson 提供的所有功能,例如Feature.IGNORE_UNKNOWN=true@JsonIgnoreProperties(ignoreUnknown = true)对我的数据 class、 csvMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)有效。 I still receive the exception column not found in schema when not all columns are declared in the schema.当架构中未声明所有列时,我仍然收到未在架构中找到的异常列。 Code works fine in the other case.代码在另一种情况下工作正常。

Here i upload my implementation在这里我上传我的实现

    public class MyClass{
        @JsonProperty("A")
        private string a;
    
        @JsonProperty("B")
        private string b;
    
        @JsonProperty("C")
        private string c;  
    }

CsvMapper mapper = new CsvMapper();
mapper.configure(Feature.IGNORE_UNKNOWN, true);
CsvSchema csvSchema;
if (disable = true) {
    schema = CsvSchema.builder()
      .addColumn("A")
      .addColumn("C")
      .build()
} else {
    schema = CsvSchema.builder()
      .addColumn("A")
      .addColumn("B")
      .addColumn("C")
      .build()
}

ObjectWriter ow = mapper.writer(schema);
String csv = ow.writeValueAsString(list);

use @JsonIgnore on top of property.在属性之上使用@JsonIgnore。 Ex:前任:

import com.fasterxml.jackson.annotation.JsonIgnore;
@Validated
@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
public class MyResponseModel {
...
        @JsonIgnore
        private String createBy;
        ...
}

Using Feature.IGNORE_UNKNOWN=true or @JsonIgnoreProperties(ignoreUnknown = true) will help when you want to deserialize JSON into your Java class.当您想将 JSON 反序列化为 Java ZA2F2ED4F9EBC2CBB4C2A1 时,使用Feature.IGNORE_UNKNOWN=true@JsonIgnoreProperties(ignoreUnknown = true)会有所帮助。

For example, this will be useful when you're implementing the REST API and in the POST method dedicated to creating new objects, you want to ignore all invalid/incorrect fields sent to you by the user and process only the valid ones instead of returning the 400/500 error.例如,当您实现 REST API 并且在专用于创建新对象的 POST 方法中,您希望忽略用户发送给您的所有无效/不正确字段并仅处理有效字段而不是返回时,这将很有用400/500 错误。

In your case, you just need to put the @JsonIgnore annotation on the field in your Java class, which you want to exclude during the serialization.在您的情况下,您只需将@JsonIgnore注释放在 Java class 中的字段上,您希望在序列化期间排除该字段。 This is an example of excluding the «a» property:这是排除 «a» 属性的示例:

public class MyClass {
    @JsonIgnore
    @JsonProperty("A")
    private String a;

    @JsonProperty("B")
    private String b;

    @JsonProperty("C")
    private String c;
}

This should also be useful in cases when you want to exclude some private and sensitive information from application logs, like passwords, PII, PHI, etc.当您想从应用程序日志中排除一些私人和敏感信息(如密码、PII、PHI 等)时,这也应该很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM