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 +}