Class EnumTranslator

java.lang.Object
org.springframework.data.rest.webmvc.json.EnumTranslator
All Implemented Interfaces:
EnumTranslationConfiguration

public class EnumTranslator extends Object implements EnumTranslationConfiguration
Configuration to tweak enum serialization.
Author:
Oliver Gierke
  • Constructor Details

  • Method Details

    • setEnableDefaultTranslation

      public void setEnableDefaultTranslation(boolean enableDefaultTranslation)
      Description copied from interface: EnumTranslationConfiguration
      Configures whether the default translation of enum names shall be applied. Defaults to true. This means the configuration will turn enum names into human friendly Strings and also parse them if - only if - no explicit translation is available.
      Specified by:
      setEnableDefaultTranslation in interface EnumTranslationConfiguration
      Parameters:
      enableDefaultTranslation - whether to enable the default translation of enum names.
    • setParseEnumNameAsFallback

      public void setParseEnumNameAsFallback(boolean parseEnumNameAsFallback)
      Description copied from interface: EnumTranslationConfiguration
      Configures whether to always accept the raw enum name when parsing. This is useful if clients were used to send the Java enum names shall not be broken even if on the serialization side enum translation is activated.
      Specified by:
      setParseEnumNameAsFallback in interface EnumTranslationConfiguration
      Parameters:
      parseEnumNameAsFallback - whether to parse the raw enum value as fallback, even if an explicit translation is available.
    • asText

      public String asText(Enum<?> value)
      Resolves the given enum value into a String consulting the configured MessageSourceAccessor potentially falling back to the default translation if configured. Returning the plain enum name if no resolution applies.
      Parameters:
      value - must not be null.
      Returns:
    • getValues

      public List<String> getValues(Class<? extends Enum<?>> type)
      Returns all potentially translated values for the given Enum type.
      Parameters:
      type - must not be null.
      Returns:
    • fromText

      public <T extends Enum<?>> T fromText(Class<T> type, String text)
      Parses the given source text into the corresponding enum value using the configured MessageSourceAccessor potentially falling back to the default translation or the plain enum name if configured.
      Parameters:
      type - must not be null.
      text - can be null
      Returns:
      the resolved enum or null if the resolution failed.