requestmapper icon indicating copy to clipboard operation
requestmapper copied to clipboard

Enum as a part of path variable

Open viartemev opened this issue 6 years ago • 1 comments

public enum  MediatekaPage {
    VIDEO("video"), AUDIO("audio"), PHOTO("photo");

    private String value;

    MediatekaPage(String value) {
        this.value = value;
    }
}
@GetMapping("/{page}")
getMediatekaPage(@PathVariable MediatekaPage page)

viartemev avatar Aug 27 '19 14:08 viartemev

There are places where binding can be done:

  1. With annotation @InitBinder
@InitBinder
public void initBinder(final WebDataBinder webdataBinder) {
    webdataBinder.registerCustomEditor(QuestionCategory.class, new QuestionCategoryConverter());
}
  1. Extends from Converter:
public class StringToEnumConverter implements Converter<String, Modes> {
 
    @Override
    public Modes convert(String from) {
        return Modes.valueOf(from);
    }
}

@Configuration
public class WebConfig implements WebMvcConfigurer {
 
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new StringToEnumConverter());
    }
}

viartemev avatar Sep 17 '19 07:09 viartemev