From dac59fbec2360eaf0624aba85e661647cf5c33c5 Mon Sep 17 00:00:00 2001 From: wugangca Date: Sun, 22 Sep 2019 20:23:27 -0600 Subject: [PATCH] BAEL-2988 Using Enums as Request Parameters in Spring (#7815) * BAEL-2988 Using Enums as Request Parameters in Spring * Remove the unused annotation based on PR feedback * Simplify code based on the PR feedback * Make the methods consistent with the article. --- .../main/java/com/baeldung/Application.java | 2 -- .../java/com/baeldung/config/MvcConfig.java | 7 ++++++ .../converter/StringToEnumConverter.java | 20 +++++++++++++++ .../com/baeldung/cors/EnumController.java | 25 +++++++++++++++++++ .../GlobalControllerExceptionHandler.java | 16 ++++++++++++ .../main/java/com/baeldung/model/Modes.java | 5 ++++ 6 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 spring-rest-simple/src/main/java/com/baeldung/config/converter/StringToEnumConverter.java create mode 100644 spring-rest-simple/src/main/java/com/baeldung/cors/EnumController.java create mode 100644 spring-rest-simple/src/main/java/com/baeldung/exceptions/GlobalControllerExceptionHandler.java create mode 100644 spring-rest-simple/src/main/java/com/baeldung/model/Modes.java diff --git a/spring-rest-simple/src/main/java/com/baeldung/Application.java b/spring-rest-simple/src/main/java/com/baeldung/Application.java index dc6bfcb970..f4147568ce 100644 --- a/spring-rest-simple/src/main/java/com/baeldung/Application.java +++ b/spring-rest-simple/src/main/java/com/baeldung/Application.java @@ -4,11 +4,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; -import org.springframework.context.annotation.ComponentScan; @EnableAutoConfiguration @SpringBootApplication -@ComponentScan("com.baeldung.cors") public class Application extends SpringBootServletInitializer { public static void main(final String[] args) { diff --git a/spring-rest-simple/src/main/java/com/baeldung/config/MvcConfig.java b/spring-rest-simple/src/main/java/com/baeldung/config/MvcConfig.java index 48b627a344..246049b722 100644 --- a/spring-rest-simple/src/main/java/com/baeldung/config/MvcConfig.java +++ b/spring-rest-simple/src/main/java/com/baeldung/config/MvcConfig.java @@ -2,6 +2,7 @@ package com.baeldung.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.format.FormatterRegistry; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; @@ -17,6 +18,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import com.baeldung.config.converter.KryoHttpMessageConverter; +import com.baeldung.config.converter.StringToEnumConverter; import java.text.SimpleDateFormat; import java.util.List; @@ -71,4 +73,9 @@ public class MvcConfig implements WebMvcConfigurer { public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**"); } + + @Override + public void addFormatters(FormatterRegistry registry) { + registry.addConverter(new StringToEnumConverter()); + } } diff --git a/spring-rest-simple/src/main/java/com/baeldung/config/converter/StringToEnumConverter.java b/spring-rest-simple/src/main/java/com/baeldung/config/converter/StringToEnumConverter.java new file mode 100644 index 0000000000..349ee5a796 --- /dev/null +++ b/spring-rest-simple/src/main/java/com/baeldung/config/converter/StringToEnumConverter.java @@ -0,0 +1,20 @@ +package com.baeldung.config.converter; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.stereotype.Component; + +import com.baeldung.model.Modes; + +@Component +public class StringToEnumConverter implements Converter { + @Override + public Modes convert(String source) { + // Remove the try-catch block if we want to handle the exception globally in GlobalControllerExceptionHandler + try { + return Modes.valueOf(source.toUpperCase()); + } catch (IllegalArgumentException e) { + return null; + } + + } +} diff --git a/spring-rest-simple/src/main/java/com/baeldung/cors/EnumController.java b/spring-rest-simple/src/main/java/com/baeldung/cors/EnumController.java new file mode 100644 index 0000000000..6bd5a16439 --- /dev/null +++ b/spring-rest-simple/src/main/java/com/baeldung/cors/EnumController.java @@ -0,0 +1,25 @@ +package com.baeldung.cors; + +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.model.Modes; + +@CrossOrigin(maxAge = 3600) +@RestController +@RequestMapping("/enums") +public class EnumController { + + @RequestMapping("/mode2str") + public String getStringToMode(@RequestParam("mode") Modes mode) { + return "good"; + } + + @RequestMapping("/findbymode/{mode}") + public String findByEnum(@PathVariable Modes mode) { + return "good"; + } +} \ No newline at end of file diff --git a/spring-rest-simple/src/main/java/com/baeldung/exceptions/GlobalControllerExceptionHandler.java b/spring-rest-simple/src/main/java/com/baeldung/exceptions/GlobalControllerExceptionHandler.java new file mode 100644 index 0000000000..78fbcf97dd --- /dev/null +++ b/spring-rest-simple/src/main/java/com/baeldung/exceptions/GlobalControllerExceptionHandler.java @@ -0,0 +1,16 @@ +package com.baeldung.exceptions; + +import org.springframework.core.convert.ConversionFailedException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +public class GlobalControllerExceptionHandler { + @ExceptionHandler(ConversionFailedException.class) + public ResponseEntity handleConflict(RuntimeException ex) { + // Remove the try-catch block in the StringToEnumConverter if we want to handle the exception here + return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST); + } +} diff --git a/spring-rest-simple/src/main/java/com/baeldung/model/Modes.java b/spring-rest-simple/src/main/java/com/baeldung/model/Modes.java new file mode 100644 index 0000000000..a82df17d7d --- /dev/null +++ b/spring-rest-simple/src/main/java/com/baeldung/model/Modes.java @@ -0,0 +1,5 @@ +package com.baeldung.model; + +public enum Modes { + ALPHA, DELTA +}