我的枚举具有以下属性:
private Integer id;
private String name;
private String description;
这个枚举具有杰克逊的@jsonvalue注释的功能:
@JsonValue
public String toValue() {
return Stream.of(values())
.filter(eventType -> eventType == this)
.findAny()
.map(EventType::toString)
.orElseThrow(() -> new IllegalArgumentException("Unable to convert EventType to value:" + this));
}
这是根据需要执行的,将枚举值序列化为ToString返回的值,这是枚举的名称。
我希望能够禁用@jsonvalue注释,并只需使用杰克逊的其他默认JSON序列化行为,该行为附加到此类中:@JsonFormat(shape = JsonFormat.Shape.OBJECT)
在某些情况下,必须获取代表整个枚举的对象而不是名称。<<<<<<<<<<<<<<<<<<<<<<<<
杰克逊似乎没有内置的能力(无论是那样还是我不够理解(。我无法创建一个扩展此类的包装类,因为Java不允许使用枚举。
有什么建议?
可以使用Mixin来实现。
保持枚举类相同,创建一个Mixin类:
// MyEnumMixin.java
import com.fasterxml.jackson.annotation.JsonValue;
public abstract class MyEnumMixin {
@JsonValue(false)
public abstract String toValue();
}
创建映射器时添加混合蛋白
// MyService.java
...
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(MyEnum.class, MyEnumMixin.class);
return mapper.writeValueAsString(pojoWithEnum);
...
借助https://stackoverflow.com/users/6911095/ldz帮助我解决了类似的问题并能够在此处添加答案。https://stackoverflow.com/a/59459177/7589862
我没有想到为此的现成解决方案,但是我能想到的最接近的选择是使用a 自定义序列化器为此特定的枚举类型。
添加自定义序列化器很容易,只需要一个人扩展JsonSerializer
类并实现serialize
方法,然后将@JsonSerialize
中的类使用需要的类型(并且您不需要@JsonValue
(。枚举对象将传递到serialize
方法中,您可以选择编写toString
或toValue
。
下面提供了一个示例代码以及输出。
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.IOException;
import java.util.stream.Stream;
public class SOQuestion {
static ThreadLocal<Boolean> USE_TO_STRING = ThreadLocal.withInitial(() -> false);
@JsonSerialize(using = MySerializer.class)
enum EventType{
ONE(1, "One", "Just One"), TWO(2, "Two", "Just Two");
private Integer id;
private String name;
private String description;
EventType(Integer id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
public String toValue() {
return Stream.of(values())
.filter(eventType -> eventType == this)
.findAny()
.map(EventType::toString)
.map(s -> "toValue="+s)
.orElseThrow(() -> new IllegalArgumentException("Unable to convert EventType to value:" + this));
}
}
@Data
@AllArgsConstructor
static class Dummy{
int someNum;
EventType eventType;
}
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
Dummy dummy = new Dummy(100, EventType.ONE);
System.out.println("**** DEFAULT *****");
System.out.println(objectMapper.writeValueAsString(dummy));
System.out.println("**** toString *****");
USE_TO_STRING.set(true);
System.out.println(objectMapper.writeValueAsString(dummy));
}
private static class MySerializer extends JsonSerializer<EventType> {
@Override
public void serialize(EventType value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
if(USE_TO_STRING.get()){
gen.writeString(value.toString());
}else{
gen.writeString(value.toValue());
}
}
}
}
输出:
**** DEFAULT *****
{"someNum":100,"eventType":"toValue=ONE"}
**** toString *****
{"someNum":100,"eventType":"ONE"}
很抱歉使用Lombok,这使事情变得简单。如果您不熟悉,它是时候接它了。