Reputation: 604
I'm having below 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.
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);
and spring.jackson.mapper.accept-case-insensitive-enums: true
. nothing worked for me.
I'm using spring boot 2.5.5.
How to accept case insensitive request with requestParam? And if requestParam is empty/null, want to set default enum as ALL.
Upvotes: 1
Views: 1874
Reputation: 91
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());
}
}
Upvotes: 6