[英]JSON deserialization to Enum with Java 8, Spring Boot Starter 2.1.9 and Lombok 1.18.10
我正在尝试反序列化为枚举,但 JSON 值(小写)与枚举常量(大写)不同。
这是枚举:
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum ContractTypes {
@JsonProperty("product")
PRODUCT("product"),
@JsonProperty("service")
SERVICE("service");
private String value;
}
如您所见,我已使用@JsonProperty
注释对元素进行注释,以尝试将 map 提供的值设置为合适的常量。
我还尝试使用@JsonValue
注释来注释属性value
。 在这两种情况下,我都得到了相同的结果:
Field error in object 'createContractRequestDto' on field 'contractType': rejected value [product]; codes [typeMismatch.createContractRequestDto.contractType,typeMismatch.contractType,typeMismatch.enm.ContractTypes,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [createContractRequestDto.contractType,contractType]; arguments []; default message [contractType]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'enm.ContractTypes' for property 'contractType'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [enm.ContractTypes] for value 'product'; nested exception is java.lang.IllegalArgumentException: No enum constant enm.ContractTypes.product]]
为什么不使用@JsonProperty
和@JsonValue
注释? 我必须如何将 map 的解决方案编码为合适的枚举元素的 JSON 值?
我们还可以创建自定义转换器。
public class ContractTypesConverter implements Converter<String, ContractTypes> {
@Override
public ContractTypes convert(String source) {
return ContractTypes.valueOf(source.toUpperCase());
}
}
这可以进一步写成这样(感谢lambda
)
Converter<String, ContractTypes> converter = source -> ContractTypes.valueOf(source.toUpperCase());
并像这样使用WebMvcConfigurer
注册它
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
// ... other configurations
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new ContractTypesConverter());
}
}
这对我有用。 不确定您在代码中遗漏了什么。
@Getter
public enum ContractTypes {
PRODUCT("product"),
SERVICE("service");
private String value;
ContractTypes(String value){
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
}
@NoArgsConstructor
@Setter
@Getter
static class Holder {
private ContractTypes contractTypes;
}
@Test
public void test() throws IOException {
Holder holder = new ObjectMapper().readValue("{\"contractTypes\":\"product\"}", Holder.class);
assertEquals(ContractTypes.PRODUCT, holder.contractTypes);
}
这些注释适用于非枚举类型的属性。 您可以像PRODUCT
一样直接使用枚举,也可以编写自定义反序列化器并将其用于反序列化注释属性。
@JsonDeserialize(using = ContractTypesDeserializer.class)
ContractTypes contractTypes;
客户解串器实现
public class ContractTypesDeserializer extends StdDeserializer<ContractTypes> {
private static final long serialVersionUID = -4714891596189L;
public ContractTypesDeserializer() {
super ContractTypes.class);
}
protected ContractTypesDeserializer(Class ContractTypes> type) {super(type);}
@Override
public ContractTypes deserialize(JsonParser parser, DeserializationContext context)
throws IOException, JsonProcessingException {
return ContractTypes.valueOf(parser.getText().toUpperCase());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.