我正在记录字段,这些字段将在更新Api呼叫期间更新. 但无论是否更新,每次都会得到道布的字段

道布属于日期型

private Date DOB;

在有效载荷{ "DOB": "1990-01-01"}中传递此参数时记录道布值

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

是因为它也有时间吗?虽然我得到了相同的时间,但仍然是打印道布即使在价值相同而没有更新的情况下也更新.为什么会这样呢?

如果我像这样在有效载荷(postman )中超过道布

 "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);
    }
}

DTO类

@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.time

java.util日期-时间API及其对应的解析/格式化类型SimpleDateFormat是过时的且容易出错.2014年3月,modern Date-Time API取代了传统的日期-时间API.从那时起,强烈建议改用Java.time,这是现代的日期-时间API.

您可以使用DateTimeFormatter#BASIC_ISO_DATE(对应于yyyyMMdd模式)来解析日期字符串,从20LocalDate212到LocalDate.

Demo:

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));
    }
}

Output:

2010-12-12

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

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

100了解现代日期-时间API

Java相关问答推荐

在未跨多次运行重写过go 的数据的情况下将数据写入到SON文件时遇到问题(使用Jackson)

JLS中形式参数列表后面的任何括号对用于确定方法结果中的精确数组类型的具体含义是什么?

我们如何直接使用kerminldap服务票证来通过ldap进行身份验证并形成LDAP上下文

当耗时的代码完成时,Circular ProgressIndicator显示得太晚

更新我们的一个文物后出现了严重的符号引用错误

Java Streams在矩阵遍历中的性能影响

查找最大子数组的和

如何将kotlin代码转换为java

Java Swing:初始化身份验证类后未检测到ATM_Interface键事件

更新GWT 2.5.1到2.11.0和sencha GXT 3.1.1到4.1时出现错误

为什么Spring Boot项目无法为基于MySQL的CRUD应用程序找到从JPARepository接口扩展的ProductRepository?

@Rollback @ Transmission在验收测试中不工作

舰队运行配置Maven版本

如何在透视表中添加对计数列的筛选?

如何在ApacheHttpClient 5中为单个请求设置代理?

Java嵌套流查找任意值

为什么项目名称出现在我的GET请求中?

java.lang.NoSuchMethodError:';org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream$Builder org.apache.poi-poi-ooxml-5.2.4

Java方法参数:括号中的类型声明?

Springboot应用程序无法识别任何@RestController或@Service,我认为@Repository也无法识别