【问题标题】:Build failed with UnrecognizedPropertyException: Unrecognized field <Section> error构建失败并出现 UnrecognizedPropertyException: Unrecognized field <Section> 错误
【发布时间】:2022-01-13 15:37:16
【问题描述】:

在我的 JSON 文件中,我将 customMetrics 定义为

"customMetricHeadings": [
  {
    "headingName": "CUSTOM VIEWABILITY",
    "customMetricCategory": "CUSTOM_VIEWABILITY"
  },
  {
    "headingName": "CUSTOM VIEWABILITY QI",
    "customMetricCategory": "CUSTOM_VIEWABILITY_QI"
  }
]

我在同一个 JSON 文件中为不同的指标定义了这个 customMetricCategory

{
  "columnName": "nonQualifiedForViewabilityAds4",
  "columnLabel": "Not viewable ads, Excl. Geo, ?????",
  "cellType": "COUNT",
  "columnWidth": "CHAR_17",
  "cellAggType": "SUM",
  "cellAggColumn": "",
  "customMetricCategory": "CUSTOM_VIEWABILITY_QI"
},
{
  "columnName": "nonQualifiedForViewabilityPct4",
  "columnLabel": "% Non quality: not viewable, Excl. Geo, ?????",
  "cellType": "TWO_PRECISION_PERCENT",
  "columnWidth": "CHAR_17",
  "cellAggType": "WEIGHTED_AVERAGE",
  "cellAggColumn": "nonQualifiedAds4",
  "customMetricCategory": "CUSTOM_VIEWABILITY_QI"
},
{
  "columnName": "nonQualifiedForBrandSafetyAds4",
  "columnLabel": "Failed brand safety ads, Excl. Geo, ?????",
  "cellType": "COUNT",
  "columnWidth": "CHAR_17",
  "cellAggType": "SUM",
  "cellAggColumn": "",
  "customMetricCategory": "CUSTOM_VIEWABILITY_QI"
},
{
  "columnName": "nonQualifiedForBrandSafetyPct4",
  "columnLabel": "% Non quality: failed brand safety, Excl. Geo, ?????",
  "cellType": "TWO_PRECISION_PERCENT",
  "columnWidth": "CHAR_17",
  "cellAggType": "WEIGHTED_AVERAGE",
  "cellAggColumn": "nonQualifiedAds4",
  "customMetricCategory": "CUSTOM_VIEWABILITY_QI"
},
{
  "columnName": "nonQualifiedForFraudAds4",
  "columnLabel": "Invalid traffic ads, Excl. Geo, ?????",
  "cellType": "COUNT",
  "columnWidth": "CHAR_17",
  "cellAggType": "SUM",
  "cellAggColumn": "",
  "customMetricCategory": "CUSTOM_VIEWABILITY_QI"
},
{
  "columnName": "nonQualifiedForFraudPct4",
  "columnLabel": "% Non quality: IVT, Excl. Geo, ?????",
  "cellType": "TWO_PRECISION_PERCENT",
  "columnWidth": "CHAR_17",
  "cellAggType": "WEIGHTED_AVERAGE",
  "cellAggColumn": "nonQualifiedAds4",
  "customMetricCategory": "CUSTOM_VIEWABILITY_QI"
},
{
  "columnName": "nonQualifiedForGeoAds4",
  "columnLabel": "Out of geo ads, Excl. Geo, ?????",
  "cellType": "COUNT",
  "columnWidth": "CHAR_17",
  "cellAggType": "SUM",
  "cellAggColumn": "",
  "customMetricCategory": "CUSTOM_VIEWABILITY_QI"
},
{
  "columnName": "nonQualifiedForGeoPct4",
  "columnLabel": "% Non quality: Out of geo, Excl. Geo, ?????",
  "cellType": "TWO_PRECISION_PERCENT",
  "columnWidth": "CHAR_17",
  "cellAggType": "WEIGHTED_AVERAGE",
  "cellAggColumn": "nonQualifiedAds4",
  "customMetricCategory": "CUSTOM_VIEWABILITY_QI"
}

在我的本地运行 repo 时,构建失败并出现以下错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CRBTemplates': Invocation of init method failed; nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "customMetricCategory" (class com.integralads.crbworker.output.template.json.TemplateColumn), not marked as ignorable (6 known properties: "columnWidth", "columnName", "cellAggColumn", "cellAggType", "cellType", "columnLabel"])
 at [Source: (URL); line: 2170, column: 32] (through reference chain: com.integralads.crbworker.output.template.json.Template["columns"]->java.util.ArrayList[205]->com.integralads.crbworker.output.template.json.TemplateColumn["customMetricCategory"])
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:139)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:414)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1248)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1168)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
        ... 98 common frames omitted
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "customMetricCategory" (class com.integralads.crbworker.output.template.json.TemplateColumn), not marked as ignorable (6 known properties: "columnWidth", "columnName", "cellAggColumn", "cellAggType", "cellType", "columnLabel"])

我是否需要为此 customMetricCategory 定义其他任何内容?我怎样才能摆脱这个错误?

TemplateColumn.java 类

package com.integralads.crbworker.output.template.json;

import com.integralads.crbworker.excel.core.ReportCell;
import com.integralads.crbworker.excel.core.ReportColumnWidth;

public class TemplateColumn {

    private String columnName;
    private String columnLabel;
    private ReportCell.CellType cellType;
    private ReportColumnWidth columnWidth;
    private ReportCell.CellAggType cellAggType;
    private String cellAggColumn;

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public String getColumnLabel() {
        return columnLabel;
    }

    public void setColumnLabel(String columnLabel) {
        this.columnLabel = columnLabel;
    }

    public ReportCell.CellType getCellType() {
        return cellType;
    }

    public void setCellType(ReportCell.CellType cellType) {
        this.cellType = cellType;
    }

    public ReportColumnWidth getColumnWidth() {
        return columnWidth;
    }

    public void setColumnWidth(ReportColumnWidth columnWidth) {
        this.columnWidth = columnWidth;
    }

    public ReportCell.CellAggType getCellAggType() {
        return cellAggType;
    }

    public void setCellAggType(ReportCell.CellAggType cellAggType) {
        this.cellAggType = cellAggType;
    }

    public String getCellAggColumn() {
        return cellAggColumn;
    }

    public void setCellAggColumn(String cellAggColumn) {
        this.cellAggColumn = cellAggColumn;
    }
}

还有一个 TemplateCustomMetricheading.java 类也为 customMetricCategory 定义

package com.integralads.crbworker.output.template.json;

public class TemplateCustomMetricHeading {
    private String headingName;
    private CustomMetricCategory customMetricCategory;

    public String getHeadingName() {
        return headingName;
    }

    public void setHeadingName(String headingName) {
        this.headingName = headingName;
    }

    public CustomMetricCategory getCustomMetricCategory() {
        return customMetricCategory;
    }

    public void setCustomMetricCategory(CustomMetricCategory customMetricCategory) {
        this.customMetricCategory = customMetricCategory;
    }
}

【问题讨论】:

  • 您似乎正在反序列化一些 POJO 列表。这个 POJO 类是否有字段 customMetricCategory 字段?
  • @AlexRudenko 是的。包 com.integralads.crbworker.output.template.json;公共枚举 CustomMetricCategory { CUSTOM_VIEWABILITY, CUSTOM_VIEWABILITY_QI }
  • 可以加TemplateColumn代码吗?谢谢!
  • @H.Tiwari,这不是字段,而是枚举的定义。 POJO 是否有该枚举字段:class TemplateColumn { CustomMetricCategory customMetricCategory; ... }
  • @AlexRudenko 我已在问题本身的末尾添加了文件。请让我知道我还缺少什么。

标签: java json


【解决方案1】:

有两种方法可以消除错误:

  1. 将缺少的字段private CustomMetricCategory customMetricCategory添加到TemplateColumn类并实现相关的getter/setter以符合输入的JSON:
public class TemplateColumn {

    private String columnName;
    private String columnLabel;
    private ReportCell.CellType cellType;
    private ReportColumnWidth columnWidth;
    private ReportCell.CellAggType cellAggType;
    private String cellAggColumn;
    private CustomMetricCategory customMetricCategory;
// ... getters/setters
}

  1. 在反序列化 TemplateColumn 类的实例时忽略未知属性的失败。
@JsonIgnoreProperties(ignoreUnknown = true)
public class TemplateColumn {
...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多