BAEL-2988 Move the code from spring-rest-simple to spring-mvc-simple-… (#7914)

* BAEL-2988 Move the code from spring-rest-simple to spring-mvc-simple-2, based on the review feedback.

* BAEL-2988 revert previous changes in the spring-rest-simple
This commit is contained in:
wugangca
2019-10-04 06:30:54 -06:00
committed by KevinGilmore
parent 5e6304f837
commit a0cbfa3b06
6 changed files with 28 additions and 15 deletions

View File

@@ -0,0 +1,22 @@
package com.baeldung.spring.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.baeldung.spring.config.converter.StringToEnumConverter;
@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
public MvcConfig() {
super();
}
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToEnumConverter());
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.spring.config.converter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import com.baeldung.spring.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,24 @@
package com.baeldung.spring.enums;
import org.springframework.web.bind.annotation.GetMapping;
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.spring.model.Modes;
@RestController
@RequestMapping("/enums")
public class EnumController {
@GetMapping("/mode2str")
public String getStringToMode(@RequestParam("mode") Modes mode) {
return "good";
}
@GetMapping("/findbymode/{mode}")
public String findByEnum(@PathVariable Modes mode) {
return "good";
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.spring.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.spring.model;
public enum Modes {
ALPHA, BETA;
}