Top Description Methods
org.springframework.core.convert

public Interface ConversionService

Known Direct Subinterfaces
org.springframework.core.convert.support.ConfigurableConversionService
Imports
org.springframework.lang.Nullable

A service interface for type conversion. This is the entry point into the convert system. Call convert(Object, Class) to perform a thread-safe type conversion using this system.
Authors
Keith Donald, Phillip Webb
Since
3.0

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if a conversion can be performed, false if not
canConvert
(Class<?>
the source type to convert from (may be null if source is null)
sourceType
,
Class<?>
the target type to convert to (required)
targetType
)

Return true if objects of sourceType can be converted to the targetType.

public boolean

Returns:

true if a conversion can be performed between the source and target types, false if not
canConvert
(TypeDescriptor
context about the source type to convert from (may be null if source is null)
sourceType
,
TypeDescriptor
context about the target type to convert to (required)
targetType
)

Return true if objects of sourceType can be converted to the targetType.

public <T> T

Returns:

the converted object, an instance of targetType
convert
(Object
the source object to convert (may be null)
source
,
Class<T>
the target type to convert to (required)
targetType
)

Convert the given source to the specified targetType.

public default Object

Returns:

the converted value
convert
(Object
the source object
source
,
TypeDescriptor
the target type
targetType
)

Convert the given source to the specified targetType.

public Object

Returns:

the converted object, an instance of targetType
convert
(Object
the source object to convert (may be null)
source
,
TypeDescriptor
context about the source type to convert from (may be null if source is null)
sourceType
,
TypeDescriptor
context about the target type to convert to (required)
targetType
)

Convert the given source to the specified targetType.

Method Detail

canConvertback to summary
public boolean canConvert(Class<?> sourceType, Class<?> targetType)

Return true if objects of sourceType can be converted to the targetType.

If this method returns true, it means convert(Object, Class) is capable of converting an instance of sourceType to targetType.

Special note on collections, arrays, and maps types: For conversion between collection, array, and map types, this method will return true even though a convert invocation may still generate a ConversionException if the underlying elements are not convertible. Callers are expected to handle this exceptional case when working with collections and maps.

Parameters
sourceType:Class<?>

the source type to convert from (may be null if source is null)

targetType:Class<?>

the target type to convert to (required)

Returns:boolean

true if a conversion can be performed, false if not

Exceptions
IllegalArgumentException:
if targetType is null
canConvertback to summary
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType)

Return true if objects of sourceType can be converted to the targetType. The TypeDescriptors provide additional context about the source and target locations where conversion would occur, often object fields or property locations.

If this method returns true, it means convert(Object, TypeDescriptor, TypeDescriptor) is capable of converting an instance of sourceType to targetType.

Special note on collections, arrays, and maps types: For conversion between collection, array, and map types, this method will return true even though a convert invocation may still generate a ConversionException if the underlying elements are not convertible. Callers are expected to handle this exceptional case when working with collections and maps.

Parameters
sourceType:TypeDescriptor

context about the source type to convert from (may be null if source is null)

targetType:TypeDescriptor

context about the target type to convert to (required)

Returns:boolean

true if a conversion can be performed between the source and target types, false if not

Exceptions
IllegalArgumentException:
if targetType is null
convertback to summary
public <T> T convert(Object source, Class<T> targetType)

Convert the given source to the specified targetType.

Parameters
source:Object

the source object to convert (may be null)

targetType:Class<T>

the target type to convert to (required)

Returns:T

the converted object, an instance of targetType

Annotations
@Nullable
Exceptions
ConversionException:
if a conversion exception occurred
IllegalArgumentException:
if targetType is null
convertback to summary
public default Object convert(Object source, TypeDescriptor targetType)

Convert the given source to the specified targetType.

Delegates to convert(Object, TypeDescriptor, TypeDescriptor) and encapsulates the construction of the source type descriptor using TypeDescriptor#forObject(Object).

Parameters
source:Object

the source object

targetType:TypeDescriptor

the target type

Returns:Object

the converted value

Annotations
@Nullable
Exceptions
ConversionException:
if a conversion exception occurred
IllegalArgumentException:
if targetType is null
Since
6.1
convertback to summary
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType)

Convert the given source to the specified targetType. The TypeDescriptors provide additional context about the source and target locations where conversion will occur, often object fields or property locations.

Parameters
source:Object

the source object to convert (may be null)

sourceType:TypeDescriptor

context about the source type to convert from (may be null if source is null)

targetType:TypeDescriptor

context about the target type to convert to (required)

Returns:Object

the converted object, an instance of targetType

Annotations
@Nullable
Exceptions
ConversionException:
if a conversion exception occurred
IllegalArgumentException:
if targetType is null, or sourceType is null but source is not null