【问题标题】:Mongo response from mongo template not being mapped properly into java object来自 mongo 模板的 Mongo 响应未正确映射到 java 对象
【发布时间】:2021-09-20 05:03:06
【问题描述】:

我有一个由 jsonSchemaPojo2 创建的 java pojo:

@JsonInclude(JsonInclude.Include.NON_DEFAULT)
@JsonPropertyOrder({
   "CardBranding"
})

public class RewardsProcessing implements Serializable
{
    @JsonProperty("CardBranding")
    @NotNull
    private String cardBranding

    public RewardsProcessing(String cardBranding){
         this.cardBranding = cardBranding
    }

    @JsonProperty("CardBranding")
    public String getCardBranding(){return cardBranding; }
    
    @JsonProperty("CardBranding")
    public void setCardBranding(String cardBranding){ this.cardBranding = cardBranding}

}

奖励收集:

[
 {CardBranding : "1"},
 {CardBranding : "2"}
]

以下代码将mongo响应解析成java pojo

AggregationResults results = mongoTemplate.aggregate(agg, "Rewards", RewardsProcessing.class);

问题是我在生成的 java 对象中得到了 null。聚合区分大小写。

我有两个选择:

  1. 使用 jsonschemapojo2 中的某些属性将 cardBranding 字段设为大写。我尝试研究但没有找到。 Schemapojo2 默认将字段设置为小写。

  2. 配置聚合方法忽略大小写。

提前致谢!!

【问题讨论】:

    标签: java mongodb spring-boot mongotemplate jsonschema2pojo


    【解决方案1】:

    不要使用jackson使用的@JsonProperty,而是使用@Field字段注释。

    @Field("CardBranding")
    private String cardBranding
    

    需要使用导入注解 import org.springframework.data.mongodb.core.mapping.Field;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-03
      • 2017-01-31
      • 1970-01-01
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      相关资源
      最近更新 更多