简体   繁体   English

Spring 启动 Rest API 枚举一些 ZD52387880E1EA22817A72D3759231 类型

[英]Spring Boot Rest API Enumerate some Java types

I am building Spring Boot webflux REST API functionality that needs to work with data containing few Java type's (let's consider String, Integer, Double for example) information as part of JSON request/responses. I am building Spring Boot webflux REST API functionality that needs to work with data containing few Java type's (let's consider String, Integer, Double for example) information as part of JSON request/responses. Attribute representing Java type must be persistable inside mongodb as well (should not be problem once JSON can work with such attribute).代表 Java 类型的属性也必须在 mongodb 内持久化(一旦 JSON 可以使用此类属性,则应该不会有问题)。 I have following model class and type enumeration which is used by REST API to serialize/deserialize JSON message's. I have following model class and type enumeration which is used by REST API to serialize/deserialize JSON message's.

@Getter
@ToString
@EqualsAndHashCode(exclude = "id")
@Document(collection = "core_scheme")
@JsonDeserialize(builder = SchemeModel.Builder.class)
@Builder(builderClassName = "Builder", toBuilder = true, setterPrefix = "with")
public class SchemeModel {

    @Id
    private final String id;

    @Field(name = "userId") private final String userId;
    @Field(name = "date") private final String creationDate;
    @Field(name = "properties") private final Map<String, SchemeTypes> properties;
}
public enum SchemeTypes {
    INTEGER, STRING, DOUBLE
}

Serialization and deserialization work's well.序列化和反序列化工作得很好。 Now the problem is that when i want to resolve real Java type's stored inside Map<String, SchemeTypes> properties map i need to do mapping similar to this (just abstraction not real code):现在的问题是,当我想解决真正的 Java 类型存储在Map<String, SchemeTypes> properties map 中时,我需要进行类似于此的映射(只是抽象而不是实际代码):

SchemeTypes.INTEGER => Java Integer class  
SchemeTypes.STRING => Java String class  
SchemeTypes.DOUBLE => Java Double class  

Is there any more simple way to represent Java type's stored inside model class and used within serialized/deserialized JSON file's so i can directly use it to deduce Java type without additional validation that it's valid Java type. Is there any more simple way to represent Java type's stored inside model class and used within serialized/deserialized JSON file's so i can directly use it to deduce Java type without additional validation that it's valid Java type. For example if type's enumarated inside mentioned enum would have exactly same naming as real Java type's i could do following without any mapping:例如,如果在提到的枚举中枚举的类型与真正的 Java 类型的命名完全相同,我可以在没有任何映射的情况下执行以下操作:

public void deduceClass(SchemeTypes type) {
    Class myClass = Class.forName(type.toString());
}

Note that i am looking for a solution which would work out of the box (i don't have to validate type's provided by user).请注意,我正在寻找一种开箱即用的解决方案(我不必验证用户提供的类型)。 If such solution would be harder to implement as mentioned mapping i will stick with mapping.如果这样的解决方案更难像提到的映射那样实现,我会坚持使用映射。

If you weren't saving this entity I could say you can actually directly map the SchemeTypes into corresponding class like following如果您不保存此实体,我可以说您实际上可以直接将 SchemeTypes map 转换为相应的 class 如下所示

public enum SchemeTypes {
    INTEGER(Integer.class), STRING(String.class), DOUBLE(Double.class);

    private final Class clazz;

    private SchemeTypes(Class clazz){
       this.clazz = clazz;
    }
    
    public Class getClazz(){
       return clazz;
    }
}

But as you are saving this it could cause some issue to deserialize.但是当你保存它时,它可能会导致一些问题反序列化。 Maybe you can save not the SchemaType instance directly but just the name of enum to overcome this like following也许您可以不直接保存 SchemaType 实例,而只保存枚举的名称来克服这个问题,如下所示

private final Map<String, String> properties;

and find the corresponding clazz value with a static method on this class like following并在此 class 上使用 static 方法找到相应的 clazz 值,如下所示

public static Class findClazzFor(String schemeTypeName){
       return SchemeTypes.valueOf(schemeTypeName).getClazz();
    }

Nevertheless I think cleanest solution would be keeping the SchemeType class instance mapping somewhere as a one-to-one map.尽管如此,我认为最干净的解决方案是将 SchemeType class 实例映射保留为一对一的 map。 And retrieve the corresponding class for provided schemeType as in the getClazz method above.并为提供的方案类型检索相应的 class,如上面的 getClazz 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM