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:
parent
f191427bea
commit
30eccf2ae8
5
spring-boot-modules/spring-boot-request-params/README.md
Normal file
5
spring-boot-modules/spring-boot-request-params/README.md
Normal file
@ -0,0 +1,5 @@
|
||||
## Spring Boot Request Params
|
||||
|
||||
This module contains articles about Spring Boot Request Params
|
||||
|
||||
### Relevant Articles:
|
28
spring-boot-modules/spring-boot-request-params/pom.xml
Normal file
28
spring-boot-modules/spring-boot-request-params/pom.xml
Normal file
@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-boot-request-params</artifactId>
|
||||
<name>spring-boot-request-params</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Module For Spring Boot Request Params</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
||||
<artifactId>spring-boot-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
@ -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";
|
||||
}
|
||||
|
||||
}
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
@ -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()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.baeldung.enummapping.enums;
|
||||
|
||||
public enum Level {
|
||||
|
||||
LOW, MEDIUM, HIGH
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.baeldung.enummapping;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import com.baeldung.enummapping.controllers.EnumMappingController;
|
||||
import com.baeldung.enummapping.enums.Level;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(EnumMappingController.class)
|
||||
public class EnumMappingIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void whenPassingLowerCaseEnumConstant_thenConvert() throws Exception {
|
||||
mockMvc.perform(get("/enummapping/get?level=medium"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(Level.MEDIUM.name()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPassingUnknownEnumConstant_thenReturnUndefined() throws Exception {
|
||||
mockMvc.perform(get("/enummapping/get?level=unknown"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("undefined"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPassingEmptyParameter_thenReturnUndefined() throws Exception {
|
||||
mockMvc.perform(get("/enummapping/get?level="))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("undefined"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPassingNoParameter_thenReturnUndefined() throws Exception {
|
||||
mockMvc.perform(get("/enummapping/get"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("undefined"));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.baeldung.enummapping.converters;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.enummapping.EnumMappingMainApplication;
|
||||
import com.baeldung.enummapping.enums.Level;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = EnumMappingMainApplication.class)
|
||||
public class StringToLevelConverterIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ConversionService conversionService;
|
||||
|
||||
@Test
|
||||
public void whenConvertStringToLevelEnumUsingCustomConverter_thenSuccess() {
|
||||
assertThat(conversionService.convert("low", Level.class)).isEqualTo(Level.LOW);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringIsEmpty_thenReturnNull() {
|
||||
assertThat(conversionService.convert("", Level.class)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringIsNull_thenReturnNull() {
|
||||
assertThat(conversionService.convert(null, Level.class)).isNull();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.baeldung.enummapping.editors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.enummapping.enums.Level;
|
||||
|
||||
public class LevelEditorIntegrationTest {
|
||||
|
||||
private final LevelEditor levelEditor = new LevelEditor();
|
||||
|
||||
@Test
|
||||
public void whenConvertStringToLevelEnumUsingCustomPropertyEditor_thenSuccess() {
|
||||
levelEditor.setAsText("lOw");
|
||||
|
||||
assertThat(levelEditor.getValue()).isEqualTo(Level.LOW);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringIsEmpty_thenReturnNull() {
|
||||
levelEditor.setAsText("");
|
||||
|
||||
assertThat(levelEditor.getValue()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStringIsNull_thenReturnNull() {
|
||||
levelEditor.setAsText(null);
|
||||
|
||||
assertThat(levelEditor.getValue()).isNull();
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user