[英]Deserialize requestParam enum case insensitive in spring boot using jackson
I'm having below api我有以下 api
@GetMapping(value = "/employees")
public List<Employee> getEmployees(
@RequestParam(value = "mode", required = false) final EmployeeMode mode) {
//calling service from here
}
I'm having EmployeeMode enum as requestParam.我将 EmployeeMode 枚举作为 requestParam。
public enum EmployeeMode {
REGULAR,
ALL,
TEMPROARY
}
I want to accept request with case insensitive.我想接受不区分大小写的请求。 Tried with
@JsonAlias
, @JsonCreator
and objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS, true);
尝试使用
@JsonAlias
、 @JsonCreator
和objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS, true);
and spring.jackson.mapper.accept-case-insensitive-enums: true
.和
spring.jackson.mapper.accept-case-insensitive-enums: true
。 nothing worked for me.没有什么对我有用。
I'm using spring boot 2.5.5.我正在使用 spring 启动 2.5.5。
How to accept case insensitive request with requestParam?如何使用 requestParam 接受不区分大小写的请求? And if requestParam is empty/null, want to set default enum as ALL.
如果 requestParam 为空/null,则要将默认枚举设置为 ALL。
You can handle it by implementing converter.您可以通过实施转换器来处理它。
public class EmployeeModeConverter implements Converter<String, EmployeeMode> {
@Override
public EmployeeMode convert(String source) {
switch (source.toUpperCase()) {
case "REGULAR": return EmployeeMode.Regular;
case "TEMPROARY": return EmployeeMode.TEMPROARY;
default: return EmployeeMode.ALL;
}
}
}
@Configuration
public class Config extends WebMvcConfigurationSupport {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new EmployeeModeConverter());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.