Spring Boot Olingo返回格式错误的DateTime或Timestamp值

beq87vna  于 2023-01-26  发布在  Spring
关注(0)|答案(2)|浏览(118)

我使用Olingo2/JPA和基于GitHub repository的spring-boot创建了一个odata服务。
问题是olingo没有按照Odata标准(或者微软标准)中定义的正确格式返回日期/时间或时间戳列。我的意思是我的前端库(即OpenUI5)希望Edm.DateTime列有<d:ValidFrom>/Date(1579075200000)/</d:ValidFrom>这样的值,但olingo返回的是<d:ValidFrom>2020-01-15T08:00:00</d:ValidFrom>,OpenUI5不接受!
你的建议是什么?
1-是否有办法强制Olingo以Microsoft定义的格式返回数据/时间或时间戳?(即/Date(timestamp)/
2-OpenUI5是否可以接受yyyy-MM-ddTHH:mm:ss的日期\时间?

vxf3dgd4

vxf3dgd41#

正如我在注解中与@Marc讨论的那样,XMLolingoyyyy-MM-ddTHH:mm:ss的格式返回data\timetimestamp列:

只要我们通过在URL中传递?$format=json来请求JSON的输出,data\time值就会以时间戳的格式返回,同时该值被/Date()字符串括起来,以显示它是data/time值。(此标准由Microsoft定义!)

UI5中,如果你要存储数据以提供给你的模拟服务器,那么需要将数据存储在JSON文件中,显然date\time值必须像/Date(timestamp)一样存储。

6bc51xsx

6bc51xsx2#

在这种情况下,您需要在JPA中创建如下字段:

@Column(name="dClas")
private Timestamp dClas;

然后将JavaType时间戳转换为相应的EdmType日期时间偏移:

public static EdmPrimitiveTypeKind getEdmPrimitiveTypeFor(Class<?> clazz) throws RuntimeException {
    EdmPrimitiveTypeKind result = types.get(clazz);
    if (result == null) {
        Optional<Class<?>> potentialType = types.keySet().stream().filter(javaClass -> javaClass.isAssignableFrom(clazz)).findFirst();
        if (potentialType.isPresent()) {
            result = types.get(potentialType.get());
        } else {
            throw new RuntimeException("Property of type " + clazz + " cannot be mapped to OData type");
        }
    }

    return result;
}

private static final Map<Class<?>, EdmPrimitiveTypeKind> types = new HashMap<>() {{
    put(String.class, EdmPrimitiveTypeKind.String);
    put(Integer.class, EdmPrimitiveTypeKind.Int32);
    put(Integer.TYPE, EdmPrimitiveTypeKind.Int32);
    put(Long.class, EdmPrimitiveTypeKind.Int64);
    put(Long.TYPE, EdmPrimitiveTypeKind.Int64);
    put(Double.class, EdmPrimitiveTypeKind.Double);
    put(Double.TYPE, EdmPrimitiveTypeKind.Double);
    put(Boolean.class, EdmPrimitiveTypeKind.Boolean);
    put(Boolean.TYPE, EdmPrimitiveTypeKind.Boolean);
    put(Float.class, EdmPrimitiveTypeKind.Single);
    put(Float.TYPE, EdmPrimitiveTypeKind.Single);
    put(Short.class, EdmPrimitiveTypeKind.Int16);
    put(Short.TYPE, EdmPrimitiveTypeKind.Int16);
    //put(BigDecimal.class, EdmPrimitiveTypeKind.Decimal);
    put(BigDecimal.class, EdmPrimitiveTypeKind.Double);
    put(Calendar.class, EdmPrimitiveTypeKind.Date);
    put(Date.class, EdmPrimitiveTypeKind.Date);
    put(LocalDate.class, EdmPrimitiveTypeKind.Date);
    put(Timestamp.class, EdmPrimitiveTypeKind.DateTimeOffset);
    put(LocalDateTime.class, EdmPrimitiveTypeKind.DateTimeOffset);
    put(byte[].class, EdmPrimitiveTypeKind.Binary);
    put(Byte[].class, EdmPrimitiveTypeKind.Binary);
    put(Blob.class, EdmPrimitiveTypeKind.Binary);
    put(Byte.class, EdmPrimitiveTypeKind.SByte);
    put(Byte.TYPE, EdmPrimitiveTypeKind.SByte);
    put(LocalTime.class, EdmPrimitiveTypeKind.TimeOfDay);
    put(Time.class, EdmPrimitiveTypeKind.TimeOfDay);
    put(Duration.class, EdmPrimitiveTypeKind.Duration);
    put(UUID.class, EdmPrimitiveTypeKind.Guid);
    put(Clob.class, EdmPrimitiveTypeKind.String);
}};

在设计Olingo OData元数据时需要进行转换。

相关问题