Class StringToEnumConverterFactory

java.lang.Object
io.leandev.appfuse.web.converter.StringToEnumConverterFactory
All Implemented Interfaces:
org.springframework.core.convert.converter.ConverterFactory<String,Enum>

public class StringToEnumConverterFactory extends Object implements org.springframework.core.convert.converter.ConverterFactory<String,Enum>

字串 → enum 的大小寫不敏感轉換(查詢參數/路徑變數)

Spring 內建的 StringToEnumConverterFactoryEnum.valueOf(String)、大小寫敏感, 因此小寫 wire 值(見 ObjectMapperBuilder.withLowercaseEnums()) 用在 @RequestParam / @PathVariable 時會被拒。註冊本 factory 後 ?status=pending_confirmation?status=PENDING_CONFIRMATION 都能解析。

語意與 Converters.convert(Object, Class) 的 enum 分支一致—— 兩者是同一條規則的兩個 surface(RSQL filter 值與 MVC 參數綁定)。

註冊方式

@Override
public void addFormatters(FormatterRegistry registry) {
    registry.addConverterFactory(new StringToEnumConverterFactory());
}
  • Constructor Details

    • StringToEnumConverterFactory

      public StringToEnumConverterFactory()
  • Method Details

    • getConverter

      public <T extends Enum> @NonNull org.springframework.core.convert.converter.Converter<String,T> getConverter(@NonNull Class<T> targetType)
      Specified by:
      getConverter in interface org.springframework.core.convert.converter.ConverterFactory<String,Enum>