Class EnumTranslator
java.lang.Object
org.springframework.data.rest.webmvc.json.EnumTranslator
- All Implemented Interfaces:
EnumTranslationConfiguration
Configuration to tweak enum serialization.
- Author:
- Oliver Gierke
-
Constructor Summary
ConstructorDescriptionEnumTranslator
(org.springframework.hateoas.mediatype.MessageResolver resolver) Creates a newEnumTranslator
using the givenMessageSourceAccessor
. -
Method Summary
Modifier and TypeMethodDescriptionResolves the given enum value into aString
consulting the configuredMessageSourceAccessor
potentially falling back to the default translation if configured.<T extends Enum<?>>
TParses the given source text into the corresponding enum value using the configuredMessageSourceAccessor
potentially falling back to the default translation or the plain enum name if configured.Returns all potentially translated values for the givenEnum
type.void
setEnableDefaultTranslation
(boolean enableDefaultTranslation) Configures whether the default translation of enum names shall be applied.void
setParseEnumNameAsFallback
(boolean parseEnumNameAsFallback) Configures whether to always accept the raw enum name when parsing.
-
Constructor Details
-
EnumTranslator
public EnumTranslator(org.springframework.hateoas.mediatype.MessageResolver resolver) Creates a newEnumTranslator
using the givenMessageSourceAccessor
.- Parameters:
resolver
- must not be null.
-
-
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 friendlyString
s and also parse them if - only if - no explicit translation is available.- Specified by:
setEnableDefaultTranslation
in interfaceEnumTranslationConfiguration
- 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 interfaceEnumTranslationConfiguration
- Parameters:
parseEnumNameAsFallback
- whether to parse the raw enum value as fallback, even if an explicit translation is available.
-
asText
Resolves the given enum value into aString
consulting the configuredMessageSourceAccessor
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
Returns all potentially translated values for the givenEnum
type.- Parameters:
type
- must not be null.- Returns:
-
fromText
Parses the given source text into the corresponding enum value using the configuredMessageSourceAccessor
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.
-