在 Spring 引导应用程序的 REST 调用中接受空字符串作为枚举
Accept empty String for Enum in REST calls in Spring Boot application
我制作了一个 Spring Boot 2 REST 应用程序。我正在使用 Angular 的 REST。我有枚举问题。
典型的枚举服务器端是:
public enum EngineType {
DIESEL, METHANE, ELECTRIC;
@Nullable
public static EngineType valueOfNullable(String value) {
try {
return valueOf(value);
} catch (Exception e) {
return null;
}
}
}
一些实体使用这些枚举作为字段,当然它们可以为空。不幸的是,当客户端为枚举发送“”(空字符串)的实体 POST 时(因为它可以为空),服务器端出现错误:
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `server.model.enums.EngineType` from String "": value not one of declared Enum instance names: [DIESEL, METHANE, ELECTRIC]; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `server.model.enums.EngineType` from String "": value not one of declared Enum instance names: [DIESEL, METHANE, ELECTRIC]
at [Source: (PushbackInputStream); line: 1, column: 153] (through reference chain: server.model.tickets.Ticket["engineType2"])
我理解消息的意义,我可以解决创建自定义反序列化器的问题:
@Component
public class EngineTypeDeserializer extends JsonDeserializer<EngineType> {
@Override
public EngineType deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
return EngineType.valueOfNullable(node.asText());
}
}
但我应该将此注释 @JsonDeserialize(using = EngineTypeDeserializer.class)
放在我的 bean 中的所有 EngineType
字段中。
我一直在寻找更好的方法来解决这个问题。你有什么建议吗?
您可以通过编程方式注册您的自定义序列化程序。
在你的 @Configuration
class:
@Bean
@Primary // Use this to shadow other objectmappers, if anny
public ObjectMapper objectMapper(){
ObjectMapper objMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(EngineType.class, new EngineTypeDeserializer());
objMapper.registerModule(module);
}
我制作了一个 Spring Boot 2 REST 应用程序。我正在使用 Angular 的 REST。我有枚举问题。
典型的枚举服务器端是:
public enum EngineType {
DIESEL, METHANE, ELECTRIC;
@Nullable
public static EngineType valueOfNullable(String value) {
try {
return valueOf(value);
} catch (Exception e) {
return null;
}
}
}
一些实体使用这些枚举作为字段,当然它们可以为空。不幸的是,当客户端为枚举发送“”(空字符串)的实体 POST 时(因为它可以为空),服务器端出现错误:
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `server.model.enums.EngineType` from String "": value not one of declared Enum instance names: [DIESEL, METHANE, ELECTRIC]; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `server.model.enums.EngineType` from String "": value not one of declared Enum instance names: [DIESEL, METHANE, ELECTRIC]
at [Source: (PushbackInputStream); line: 1, column: 153] (through reference chain: server.model.tickets.Ticket["engineType2"])
我理解消息的意义,我可以解决创建自定义反序列化器的问题:
@Component
public class EngineTypeDeserializer extends JsonDeserializer<EngineType> {
@Override
public EngineType deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
return EngineType.valueOfNullable(node.asText());
}
}
但我应该将此注释 @JsonDeserialize(using = EngineTypeDeserializer.class)
放在我的 bean 中的所有 EngineType
字段中。
我一直在寻找更好的方法来解决这个问题。你有什么建议吗?
您可以通过编程方式注册您的自定义序列化程序。
在你的 @Configuration
class:
@Bean
@Primary // Use this to shadow other objectmappers, if anny
public ObjectMapper objectMapper(){
ObjectMapper objMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(EngineType.class, new EngineTypeDeserializer());
objMapper.registerModule(module);
}