BAEL-5978: Case Insensitive Enum Mapping in Spring Boot (#13061)
* BAEL-5978: Case Insensitive Enum Mapping in Spring Boot * Move code to a new module: spring-boot-request-params
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.enummapping;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class EnumMappingMainApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(EnumMappingMainApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.enummapping.config;
|
||||
|
||||
import org.springframework.boot.convert.ApplicationConversionService;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.format.FormatterRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import com.baeldung.enummapping.converters.StringToLevelConverter;
|
||||
|
||||
@Configuration
|
||||
public class EnumMappingConfig implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void addFormatters(FormatterRegistry registry) {
|
||||
ApplicationConversionService.configure(registry);
|
||||
registry.addConverter(new StringToLevelConverter());
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.enummapping.controllers;
|
||||
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.enummapping.editors.LevelEditor;
|
||||
import com.baeldung.enummapping.enums.Level;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("enummapping")
|
||||
public class EnumMappingController {
|
||||
|
||||
@InitBinder
|
||||
public void initBinder(WebDataBinder dataBinder) {
|
||||
dataBinder.registerCustomEditor(Level.class, new LevelEditor());
|
||||
}
|
||||
|
||||
@GetMapping("/get")
|
||||
public String getByLevel(@RequestParam(required = false) Level level) {
|
||||
if (level != null) {
|
||||
return level.name();
|
||||
}
|
||||
return "undefined";
|
||||
}
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.enummapping.converters;
|
||||
|
||||
import org.apache.commons.lang3.EnumUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
import com.baeldung.enummapping.enums.Level;
|
||||
|
||||
public class StringToLevelConverter implements Converter<String, Level> {
|
||||
|
||||
@Override
|
||||
public Level convert(String source) {
|
||||
if (StringUtils.isBlank(source)) {
|
||||
return null;
|
||||
}
|
||||
return EnumUtils.getEnum(Level.class, source.toUpperCase());
|
||||
}
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.enummapping.editors;
|
||||
|
||||
import java.beans.PropertyEditorSupport;
|
||||
|
||||
import org.apache.commons.lang3.EnumUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.baeldung.enummapping.enums.Level;
|
||||
|
||||
public class LevelEditor extends PropertyEditorSupport {
|
||||
|
||||
@Override
|
||||
public void setAsText(String text) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
setValue(null);
|
||||
} else {
|
||||
setValue(EnumUtils.getEnum(Level.class, text.toUpperCase()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.enummapping.enums;
|
||||
|
||||
public enum Level {
|
||||
|
||||
LOW, MEDIUM, HIGH
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user