Spring Boot Controller - Enum parameter as value

I have a spring boot controller, one of my options is Enum. Enumeration has string value. I want to pass the enum and controller value as a parameter to give me Enum. It can be done?

@RequestMapping(value = "/") public MyResponse getResponse ( @RequestParam(value = "version") final ProjectVersion version ) { ...bla bla bla... } public enum ProjectVersion { VERSION_1 ("1.00") VERSION_2 ("2.00") private final String version; ProjectVersion ( String version ) { this.version = version; } @Override public String toString() { return this.version; } } 

I want to be able to make a request as follows:

http://myhost.com/mypath?version=1.00

And get ProjectVersion.VERSION_1 in the controller

Any ideas?

+5
source share
2 answers

It is impossible as it is. You must create your own converter to convert from String to ProjectVersion .

For example, the converter from String to ProjectVersion first defined:

 public class ProjectVersionConverter implements ConditionalGenericConverter { @Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { return targetType.getType().equals(ProjectVersion.class); } @Override public Set<ConvertiblePair> getConvertibleTypes() { return Collections.singleton(new ConvertiblePair(String.class , ProjectVersion.class)); } @Override public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { return ProjectVersion.findByVersion((String)source); } } 

Then register it:

 @Configuration public class CustomWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter { @Override public void addFormatters(FormatterRegistry registry) { registry.addConverter(new ProjectVersionConverter()); } } 

You can skip registration if you have defined a ProjectVersionConverter that has a Spring bean. (This code has not been tested).

+7
source

Since you need to pass an argument (for each request) to the Controller method, a clean solution is to use HandlerMethodArgumentResolver , so that the Spring Container can dynamically inject your ProjectVersion argument into the Controller method , as shown below:

ProjectVersionArgumentResolver class:

 public class ProjectVersionArgumentResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter methodParameter) { return methodParameter.getParameterType().equals(ProjectVersion.class); } @Override public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception { return ProjectVersion.fromString(nativeWebRequest.getParameter("version")); } } 

Spring-boot class ApplicationLauncher:

 public class MyProjectApplicationLauncher extends WebMvcConfigurerAdapter { @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { //add the new resolver argumentResolvers.add(new MyMethodArgumentResolver()); } public static void main(String[] args) { SpringApplication.run(MyProjectApplicationLauncher.class, args); } } 

ProjectVersion class:

 public enum ProjectVersion { //add your existing code //Add fromString method to convert string to enum public static ProjectVersion fromString(String input) { for (ProjectVersion projectVersion : ProjectVersion.values()) { if (projectVersion.version.equals(input)) { return projectVersion; } } return null; } } 
+2
source

Source: https://habr.com/ru/post/1266404/


All Articles