Class PathVariableMethodArgumentResolver
java.lang.Object
org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver
org.springframework.web.servlet.mvc.method.annotation.PathVariableMethodArgumentResolver
- All Implemented Interfaces:
HandlerMethodArgumentResolver
,UriComponentsContributor
public class PathVariableMethodArgumentResolver
extends AbstractNamedValueMethodArgumentResolver
implements UriComponentsContributor
Resolves method arguments annotated with an @
PathVariable
.
An @PathVariable
is a named value that gets resolved from a URI template variable.
It is always required and does not have a default value to fall back on. See the base class
AbstractNamedValueMethodArgumentResolver
for more information on how named values are processed.
If the method parameter type is Map
, the name specified in the annotation is used
to resolve the URI variable String value. The value is then converted to a Map
via
type conversion, assuming a suitable Converter
or PropertyEditor
has been
registered.
A WebDataBinder
is invoked to apply type conversion to resolved path variable
values that don't yet match the method parameter type.
- Since:
- 3.1
- Author:
- Rossen Stoyanchev, Arjen Poutsma, Juergen Hoeller
-
Nested Class Summary
Nested classes/interfaces inherited from class org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver
AbstractNamedValueMethodArgumentResolver.NamedValueInfo
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
contributeMethodArgument
(MethodParameter parameter, Object value, UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) Process the given method argument and either update theUriComponentsBuilder
or add to the map with URI variables to use to expand the URI after all arguments are processed.createNamedValueInfo
(MethodParameter parameter) Create theAbstractNamedValueMethodArgumentResolver.NamedValueInfo
object for the given method parameter.protected String
formatUriValue
(ConversionService cs, TypeDescriptor sourceType, Object value) protected void
handleMissingValue
(String name, MethodParameter parameter) Invoked when a named value is required, butAbstractNamedValueMethodArgumentResolver.resolveName(String, MethodParameter, NativeWebRequest)
returnednull
and there is no default value.protected void
handleMissingValueAfterConversion
(String name, MethodParameter parameter, NativeWebRequest request) Invoked when a named value is present but becomesnull
after conversion.protected void
handleResolvedValue
(Object arg, String name, MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest request) Invoked after a value is resolved.protected Object
resolveName
(String name, MethodParameter parameter, NativeWebRequest request) Resolve the given parameter type and value name into an argument value.boolean
supportsParameter
(MethodParameter parameter) Whether this contributor supports the given method parameter.Methods inherited from class org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver
handleMissingValue, resolveArgument
-
Constructor Details
-
PathVariableMethodArgumentResolver
public PathVariableMethodArgumentResolver()
-
-
Method Details
-
supportsParameter
Description copied from interface:UriComponentsContributor
Whether this contributor supports the given method parameter.- Specified by:
supportsParameter
in interfaceHandlerMethodArgumentResolver
- Specified by:
supportsParameter
in interfaceUriComponentsContributor
- Parameters:
parameter
- the method parameter to check- Returns:
true
if this resolver supports the supplied parameter;false
otherwise
-
createNamedValueInfo
protected AbstractNamedValueMethodArgumentResolver.NamedValueInfo createNamedValueInfo(MethodParameter parameter) Description copied from class:AbstractNamedValueMethodArgumentResolver
Create theAbstractNamedValueMethodArgumentResolver.NamedValueInfo
object for the given method parameter. Implementations typically retrieve the method annotation by means ofMethodParameter.getParameterAnnotation(Class)
.- Specified by:
createNamedValueInfo
in classAbstractNamedValueMethodArgumentResolver
- Parameters:
parameter
- the method parameter- Returns:
- the named value information
-
resolveName
@Nullable protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception Description copied from class:AbstractNamedValueMethodArgumentResolver
Resolve the given parameter type and value name into an argument value.- Specified by:
resolveName
in classAbstractNamedValueMethodArgumentResolver
- Parameters:
name
- the name of the value being resolvedparameter
- the method parameter to resolve to an argument value (pre-nested in case of aOptional
declaration)request
- the current request- Returns:
- the resolved argument (may be
null
) - Throws:
Exception
- in case of errors
-
handleMissingValue
protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException Description copied from class:AbstractNamedValueMethodArgumentResolver
Invoked when a named value is required, butAbstractNamedValueMethodArgumentResolver.resolveName(String, MethodParameter, NativeWebRequest)
returnednull
and there is no default value. Subclasses typically throw an exception in this case.- Overrides:
handleMissingValue
in classAbstractNamedValueMethodArgumentResolver
- Parameters:
name
- the name for the valueparameter
- the method parameter- Throws:
ServletRequestBindingException
-
handleMissingValueAfterConversion
protected void handleMissingValueAfterConversion(String name, MethodParameter parameter, NativeWebRequest request) throws Exception Description copied from class:AbstractNamedValueMethodArgumentResolver
Invoked when a named value is present but becomesnull
after conversion.- Overrides:
handleMissingValueAfterConversion
in classAbstractNamedValueMethodArgumentResolver
- Parameters:
name
- the name for the valueparameter
- the method parameterrequest
- the current request- Throws:
Exception
-
handleResolvedValue
protected void handleResolvedValue(@Nullable Object arg, String name, MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest request) Description copied from class:AbstractNamedValueMethodArgumentResolver
Invoked after a value is resolved.- Overrides:
handleResolvedValue
in classAbstractNamedValueMethodArgumentResolver
- Parameters:
arg
- the resolved argument valuename
- the argument nameparameter
- the argument parameter typemavContainer
- theModelAndViewContainer
(may benull
)request
- the current request
-
contributeMethodArgument
public void contributeMethodArgument(MethodParameter parameter, Object value, UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) Description copied from interface:UriComponentsContributor
Process the given method argument and either update theUriComponentsBuilder
or add to the map with URI variables to use to expand the URI after all arguments are processed.- Specified by:
contributeMethodArgument
in interfaceUriComponentsContributor
- Parameters:
parameter
- the controller method parameter (nevernull
)value
- the argument value (possiblynull
)builder
- the builder to update (nevernull
)uriVariables
- a map to add URI variables to (nevernull
)conversionService
- a ConversionService to format values as Strings
-
formatUriValue
@Nullable protected String formatUriValue(@Nullable ConversionService cs, @Nullable TypeDescriptor sourceType, Object value)
-