使用@JsonProperty将多个类似数据源写在一起

一、需求

原来已经有了自爬取的APP列表解析信息,现在增加一个渠道的APP列表,字段意思一样,但是字段名不一样。为了方便数据存储不变和下游部门代码不变,决定存储在一起,这就需要将字段映射成一样的。

二、实现

1、原来代码

package com.credithc.sea.entity.deviceinfo;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AppListInfo extends DeviceInfoBase {

    private List apps = new ArrayList<>();

    public List getApps() {
        return apps;
    }

    public void setApps(List apps) {
        if (apps == null)
            this.apps = Collections.emptyList();
        else
            this.apps = apps;
    }

    public static class AppInfo {
        @JsonProperty(value = "app_label")
        private String appLabel; //App名称
        @JsonProperty(value = "pkg_name")
        private String pkgName; //App包名
        @JsonProperty(value = "firstInstallTime")
        private String firstInstallTime;
        @JsonProperty(value = "lastUpdateTime")
        private String lastUpdateTime;

        public String getFirstInstallTime() {
            return firstInstallTime;
        }

        public void setFirstInstallTime(String firstInstallTime) {
            this.firstInstallTime = firstInstallTime;
        }

        public String getLastUpdateTime() {
            return lastUpdateTime;
        }

        public void setLastUpdateTime(String lastUpdateTime) {
            this.lastUpdateTime = lastUpdateTime;
        }

        public String getAppLabel() {
            return appLabel;
        }

        public void setAppLabel(String appLabel) {
            this.appLabel = appLabel;
        }

        public String getPkgName() {
            return pkgName;
        }

        public void setPkgName(String pkgName) {
            this.pkgName = pkgName;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (!(o instanceof AppInfo)) {
                return false;
            }

            AppInfo appInfo = (AppInfo) o;

            if (appLabel != null ? !appLabel.equals(appInfo.appLabel) : appInfo.appLabel != null) {
                return false;
            }
            if (firstInstallTime != null ? !firstInstallTime.equals(appInfo.firstInstallTime) : appInfo.firstInstallTime != null) {
                return false;
            }
            if (lastUpdateTime != null ? !lastUpdateTime.equals(appInfo.lastUpdateTime) : appInfo.lastUpdateTime != null) {
                return false;
            }
            return pkgName != null ? pkgName.equals(appInfo.pkgName) : appInfo.pkgName == null;
        }

        @Override
        public int hashCode() {
            int result = appLabel != null ? appLabel.hashCode() : 0;
            result = 31 * result + (pkgName != null ? pkgName.hashCode() : 0);
            return result;
        }
    }

}

对应需要解析的数据是:

{
  "params": {
    "crawler_time": "2020-01-02 08:20:22",
    "biz_channel": "API001",
    "user_id": "0000000001",
    "idCard": "证件号码",
    "app_ver": "app_ver",
    "biz_type": "ISS001",
    "imei": "imei",
    "crawler_type": "lend",
    "type": "APP_LIST",
    "apps": [
      {
        "app_label": "Solusindo",
        "pkg_name": "com.ecreditpal.solusindo"
      },
      {
        "app_label": "搜狗浏览器",
        "pkg_name": "sogou.mobile.explorer"
      }
    ]
  }
}

后来需要解析的是:

{
  "params": {
    "crawler_time": "2020-01-02 08:20:22",
    "biz_channel": "API001",
    "user_id": "0000000002",
    "idCard": "证件号码",
    "app_ver": "app_ver",
    "biz_type": "ISS001",
    "imei": "imei",
    "crawler_type": "lend",
    "type": "APP_LIST",
    "apps": [
      {
        "app_name": "Solusindo",
        "package_name": "com.ecreditpal.solusindo"
      },
      {
        "app_label": "搜狗浏览器",
        "pkg_name": "sogou.mobile.explorer"
      }
    ]
  }
}

可以看到,APP名原来是app_label、pkg_name,新渠道是app_name、package_name,需要统一存储为app_label、pkg_name。

实现:

package com.credithc.sea.entity.deviceinfo;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AppListInfo extends DeviceInfoBase {

    private List apps = new ArrayList<>();

    public List getApps() {
        return apps;
    }

    public void setApps(List apps) {
        if (apps == null)
            this.apps = Collections.emptyList();
        else
            this.apps = apps;
    }

    public static class AppInfo {
        @JsonProperty(value = "app_label")
        private String appLabel; //App名称
        @JsonProperty(value = "pkg_name")
        private String pkgName; //App包名
        @JsonProperty(value = "firstInstallTime")
        private String firstInstallTime;
        @JsonProperty(value = "lastUpdateTime")
        private String lastUpdateTime;

        public String getFirstInstallTime() {
            return firstInstallTime;
        }

        public void setFirstInstallTime(String firstInstallTime) {
            this.firstInstallTime = firstInstallTime;
        }

        public String getLastUpdateTime() {
            return lastUpdateTime;
        }

        public void setLastUpdateTime(String lastUpdateTime) {
            this.lastUpdateTime = lastUpdateTime;
        }

        public String getAppLabel() {
            return appLabel;
        }

        public void setAppLabel(String appLabel) {
            this.appLabel = appLabel;
        }

        @JsonProperty(value = "app_name")
        public void setAppName(String appName) {
            this.appLabel = appName;
        }

        @JsonProperty(value ="package_name")
        public void packageName(String packageName) {
            this.pkgName = packageName;
        }

        public String getPkgName() {
            return pkgName;
        }

        public void setPkgName(String pkgName) {
            this.pkgName = pkgName;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (!(o instanceof AppInfo)) {
                return false;
            }

            AppInfo appInfo = (AppInfo) o;

            if (appLabel != null ? !appLabel.equals(appInfo.appLabel) : appInfo.appLabel != null) {
                return false;
            }
            if (firstInstallTime != null ? !firstInstallTime.equals(appInfo.firstInstallTime) : appInfo.firstInstallTime != null) {
                return false;
            }
            if (lastUpdateTime != null ? !lastUpdateTime.equals(appInfo.lastUpdateTime) : appInfo.lastUpdateTime != null) {
                return false;
            }
            return pkgName != null ? pkgName.equals(appInfo.pkgName) : appInfo.pkgName == null;
        }

        @Override
        public int hashCode() {
            int result = appLabel != null ? appLabel.hashCode() : 0;
            result = 31 * result + (pkgName != null ? pkgName.hashCode() : 0);
            return result;
        }
    }

}

增加代码:

        @JsonProperty(value = "app_name")
        public void setAppName(String appName) {
            this.appLabel = appName;
        }

        @JsonProperty(value ="package_name")
        public void packageName(String packageName) {
            this.pkgName = packageName;
        }

存数结果:

{
  "queryDate": "2020-01-17 15:28:44",
  "type": "APP_LIST",
  "idCard": "证件号码",
  "imei": "imei",
  "apps": [
    {
      "app_label": "Solusindo",
      "pkg_name": "com.ecreditpal.solusindo",
      "firstInstallTime": null,
      "lastUpdateTime": null
    },
    {
      "app_label": "搜狗浏览器",
      "pkg_name": "sogou.mobile.explorer",
      "firstInstallTime": null,
      "lastUpdateTime": null
    }
  ],
  "user_id": "0000000002",
  "biz_channel": "API001",
  "biz_type": "ISS001",
  "app_ver": "app_ver",
  "crawler_time": "2020-01-02 08:20:22",
  "crawler_type": "lend"
}

 

你可能感兴趣的:(ES,JAVA,代码实践)