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

    • EnumTranslator

      public EnumTranslator(org.springframework.hateoas.mediatype.MessageResolver resolver)
      Creates a new EnumTranslator using the given MessageSourceAccessor.
      Parameters:
      resolver - must not be null.
  • Method Details

    • setEnableDefaultTranslation

      public void setEnableDefaultTranslation(boolean enableDefaultTranslation)
      Specified by:
      setEnableDefaultTranslation in interface EnumTranslationConfiguration
    • setParseEnumNameAsFallback

      public void setParseEnumNameAsFallback(boolean parseEnumNameAsFallback)
      Specified by:
      setParseEnumNameAsFallback in interface EnumTranslationConfiguration
    • 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.