简体   繁体   English

如何从Java中的Enum值返回具有所有属性的Enum对象

[英]How to Return Enum objects with all properties from Enum values in Java

I have got a scenario to return list of status as json which are available in Status Enum, My Enum Looks below 我有一个方案以状态列表的形式返回状态列表为json,状态枚举在下面显示

Example:- 例:-

public enum Status {

CREATED("100", "CREATED"), UPDATED("200", "UPDATED"), DELETED("300", "DELETED");

private final String id;
private final String name;

private Status(String id, String name) {
    this.id = id;
    this.name = name;
}

public String getId() {
    return id;
}

public String getName() {
    return name;
}

@Override
public String toString() {
    return name;
}

public List<Map<String, String>> lookup() {
    List<Map<String, String>> list = new ArrayList<>();
    for (Status s : values()) {
        Map<String, String> map = new HashMap<>();
        map.put("ID", s.getId());
        map.put("name", s.getName());
        list.add(map);
    }
    return list;

}
}

need the output like this: 需要这样的输出:

[{id:"100",name:"CREATED"},{id:"200", name:"UPDATED"}...] I have written lookup method with List Of maps to build the response, Is there any better way or utility to convert the Enum to Object with all the properties available in Enum. [{id:“ 100”,名称:“ CREATED”},{id:“ 200”,名称:“ UPDATED”} ......]我编写了带有List Of maps的查找方法来构建响应,还有什么更好的方法吗?用枚举中所有可用属性将枚举转换为对象的方法或实用程序。

Is there any better way to do this? 有什么更好的方法吗?

You can use a library for JSON serialization like Gson or Jackson , and implement custom serialization in it. 您可以使用GsonJackson之类的库来进行JSON序列化,并在其中实现自定义序列化。

Example Gson custom serializer : Gson自定义序列化器示例:

class StatusSerializer implements JsonSerializer<Status> {
    public JsonElement serialize(Status status, Type typeOfSrc, JsonSerializationContext context) {
        JsonObject object = new JsonObject();
        object.addProperty("ID", status.getId());
        object.addProperty("name", status.getName());
        return object;
    }
}

You then use it as follows: 然后,按如下方式使用它:

Gson gson = new GsonBuilder()
        .registerTypeAdapter(Status.class, new StatusSerializer())
        .create();
String json = gson.toJson(Status.CREATED);

which yields: 产生:

{"ID":"100","name":"CREATED"}

You can use Jackson to convert to JSON. 您可以使用Jackson转换为JSON。 Just include @JsonFormat(shape = Shape.OBJECT) at Enum declaration. 只需在Enum声明中包含@JsonFormat(shape = Shape.OBJECT)。 This should give you the result. 这应该给您结果。

Arrays.stream( Status.class.getEnumConstants() )
   .map( e -> Map.of( "id", e.getId(), "name", e.getName() )
   .collect( Collectors.toList() );

I'm on mobile and can't test but you get the general idea 我在移动设备上,无法测试,但是您了解了一般想法

You can use Stream API in Java 8. And use parallel to good perfomance. 您可以在Java 8中使用Stream API。并可以并行使用以达到良好的性能。

List<Map<String, String>> list = Stream.of(Status.values()).parallel().map(temp -> {
        Map<String, String> obj = new HashMap<String, String>();
        obj.put("id", temp.getId());
        obj.put("name", temp.getName());
        return obj;
    }).collect(Collectors.toList());

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

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