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.
This commit is contained in:
wugangca 2019-09-22 20:23:27 -06:00 committed by KevinGilmore
parent 925b93b91c
commit dac59fbec2
6 changed files with 73 additions and 2 deletions

View File

@ -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) {

View File

@ -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());
}
}

View File

@ -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<String, Modes> {
@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;
}
}
}

View File

@ -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";
}
}

View File

@ -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<String> 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);
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.model;
public enum Modes {
ALPHA, DELTA
}