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:
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.spring.model;
|
||||
|
||||
public enum Modes {
|
||||
ALPHA, BETA;
|
||||
}
|
||||
Reference in New Issue
Block a user