2

我已经在 java 中实现了 LocalDateTime,但是当我在两台服务器上运行应用程序时,它在自由服务器中将 JSON 日期时间作为字符串返回,但在 Websphere 服务器中,日期时间作为对象而不是字符串返回。有没有解决方案,我在数据库中有这个日期时间:2001-11-12 17:18:19.123456。下面是代码:

@Converter(autoApply = true)
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {

    @Override
    public Timestamp convertToDatabaseColumn(LocalDateTime locDateTime) {
        return locDateTime == null ? null : Timestamp.valueOf(locDateTime);
    }

    @Override
    public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
        return sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime();
    }
}

public class LocalDateTimeAdapter extends XmlAdapter<String, LocalDateTime> {
private static DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS");

@Override
public LocalDateTime unmarshal(String string) throws Exception {
    synchronized (FORMATTER) {
        return LocalDateTime.parse(string, FORMATTER);
    }
}

@Override
public String marshal(LocalDateTime localDateTime) throws Exception {
    synchronized (FORMATTER) {
        return localDateTime.format(FORMATTER);
    }
}
}

//Entity looks like this
@Column(name = "date_time")
    @Convert(converter = LocalDateTimeAttributeConverter.class)
    private LocalDateTime startDate;

//The DTO looks like this
@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
    private LocalDateTime startDate;


//Websphere returns 
startDate: {year: 2001, monthValue: 12, month: "DECEMBER", dayOfMonth: 12, dayOfYear: .., …}

//instead of 
"2001-11-12 17:18:19.123456"

如何解决这个问题?

4

0 回答 0