为什么Java更新API时每次更新或未更新都要记录DOB?

问题描述 投票:0回答:2

我正在记录将在更新 API 调用期间更新的字段。 但是无论更新与否,每次都会获取DOB字段

DOB 是日期类型

private Date DOB;

在有效负载中传递 DOB VALUE 时记录 DOB VALUE

{ "DOB": "1990-01-01"}

dob: Mon Jan 01 05:30:00 IST 1990
Updated Fields during request: DOB  

是因为它也有时间吗?尽管我对每个请求的时间都相同,但即使值相同且未更新,DOB 也会更新。为什么会这样?

如果我像这样在有效负载(邮递员)中传递 DOB:

 "DOB": "20101212"

//it is printing only this 1970 value on log console
dob: Thu Jan 01 11:05:01 IST 1970 

我该如何处理这些情况?

代码

public static class Updater {
  private final OrgEmployeeMap existing;
  private final EmployeeInformation information;
  private final Set<String> updatedFields = new HashSet<>();

  public Updater(OrgEmployeeMap existing, EmployeeInformation information) {
    this.existing = existing;
    this.information = information;
  }

  public <T> Updater update(String fieldName,
                            Function<EmployeeInformation, T> newValueGetter,
                            Function<OrgEmployeeMap, T> oldValueGetter,
                            BiConsumer<OrgEmployeeMap, T> oldValueSetter) {
    final var newValue = newValueGetter.apply(information);
    final var oldValue = oldValueGetter.apply(existing);
    if (newValue == null || newValue.equals(oldValue)) {
      return this;
    }
    this.updatedFields.add(fieldName);
    oldValueSetter.accept(existing, newValue);
    return this;
  }

  public Set<String> getUpdatedFields() {
    return updatedFields;
  }
}
private void updateExistingEmployee(OrgEmployeeMap existingOrgEmployee, RequestMessage requestMessage) {
    try {
        EmployeeInformation employeeInfo = requestMessage.getData().get(0).getDemographicData().getEmployeeInformation().get(0);
        final var updater = new Updater(existingOrgEmployee, employeeInfo);

        updater
            .update("FirstName", EmployeeInformation::getEmployeeFirstName, OrgEmployeeMap::getFirstName, OrgEmployeeMap::setFirstName)
            .update("LastName", EmployeeInformation::getEmployeeLastName, OrgEmployeeMap::getLastName, OrgEmployeeMap::setLastName)
            .update("DOB", EmployeeInformation::getDOB, OrgEmployeeMap::getDob, OrgEmployeeMap::setDob)
            .update("Gender", EmployeeInformation::getGender, OrgEmployeeMap::getGender, OrgEmployeeMap::setGender)
            .update("Email", EmployeeInformation::getEmail, OrgEmployeeMap::getEmail, OrgEmployeeMap::setEmail);

        final var modifiedFields = updater.getUpdatedFields();
        if (!modifiedFields.isEmpty()) {
            log.info("Updated employee : " + String.join(", ", modifiedFields));
        }

        orgEmployeeMapRepository.save(existingOrgEmployee);

        log.info("Updated Employee : " + existingOrgEmployee);
    } catch (JSONException e) {
        log.error("error in processing the request error", e.getMessage());
    } catch (Exception ex) {
        log.error("An unexpected error occurred: " + ex.getMessage(), ex);
    }
}

去上课

@Data
public class EmployeeInformation {

    @JsonProperty(value = "Email")
    private String Email;

    @JsonProperty(value = "EmployeeFirstName")
    private String EmployeeFirstName;

    @JsonProperty(value = "EmployeeLastName")
    private String EmployeeLastName;

    @JsonProperty(value = "DOB")
    private Date DOB;

..
}

模特班

@Data
@Entity
@Table(name = "employee")
public class EmployeeMap {
@Id
    @Column(name = "Id", length = 100, nullable = false)
    private String Id;

    @Column(name = "firstName", length = 100, nullable = false)
    private String firstName;

    @Column(name = "lastName", length = 100)
    private String lastName;

    @Column(name = "email", length = 100, nullable = false)
    private String email;

    @Column(name = "gender")
    private String gender;

    @Column(name = "dob", nullable = false)
    private Date dob;
}
java json spring-boot date-parsing
2个回答
1
投票

java.time

java.util
日期时间 API 及其相应的解析/格式化类型
SimpleDateFormat
已过时且容易出错。 2014 年 3 月,现代日期时间 API 取代了旧版日期时间 API。从那时起,强烈建议切换到现代日期时间 API
java.time

您可以使用

DateTimeFormatter#BASIC_ISO_DATE
(对应于 yyyyMMdd 模式)将日期字符串 20101212 解析为
LocalDate

演示:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

class Main {
    public static void main(String[] args) {
        System.out.println(LocalDate.parse("20101212", DateTimeFormatter.BASIC_ISO_DATE));
    }
}

输出:

2010-12-12

根据此描述,您可以在代码中进行以下更改:

@JsonProperty(value = "DOB")
@JsonFormat(pattern = "yyyyMMdd", shape = JsonFormat.Shape.STRING)
private LocalDate DOB;

Trail:日期时间

了解现代日期时间 API

0
投票

我会稍微关注线条

if (newValue == null || newValue.equals(oldValue)) {
  return this;
}
this.updatedFields.add(fieldName);

由于某种原因,每次 dob 都会调用updatedField.add(...)。 因此,newValue 为 null 或 equals 时可能发生的情况无法按预期工作。应用调试会话或添加更多日志输出,以便您了解比较失败的原因。

© www.soinside.com 2019 - 2024. All rights reserved.