@ -50,6 +50,8 @@ import org.springframework.web.server.ServerWebExchange;
@@ -50,6 +50,8 @@ import org.springframework.web.server.ServerWebExchange;
/ * *
* Supports the invocation of { @code @RequestMapping } methods .
*
* @author Rossen Stoyanchev
* /
public class RequestMappingHandlerAdapter implements HandlerAdapter , BeanFactoryAware , InitializingBean {
@ -57,9 +59,11 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, BeanFactory
@@ -57,9 +59,11 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, BeanFactory
private static Log logger = LogFactory . getLog ( RequestMappingHandlerAdapter . class ) ;
private final List < HandlerMethodArgumentResolver > argumentResolvers = new ArrayList < > ( ) ;
private List < HandlerMethodArgumentResolver > customArgumentResolvers ;
private List < HandlerMethodArgumentResolver > argumentResolvers ;
private final List < HttpMessageConverter < ? > > messageConverters = new ArrayList < > ( ) ;
private final List < HttpMessageConverter < ? > > messageConverters = new ArrayList < > ( 10 ) ;
private ConversionService conversionService = new DefaultFormattingConversionService ( ) ;
@ -75,13 +79,26 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, BeanFactory
@@ -75,13 +79,26 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, BeanFactory
}
/ * *
* Provide custom argument resolvers without overriding the built - in ones .
* /
public void setCustomArgumentResolvers ( List < HandlerMethodArgumentResolver > argumentResolvers ) {
this . customArgumentResolvers = argumentResolvers ;
}
/ * *
* Return the custom argument resolvers .
* /
public List < HandlerMethodArgumentResolver > getCustomArgumentResolvers ( ) {
return this . customArgumentResolvers ;
}
/ * *
* Configure the complete list of supported argument types thus overriding
* the resolvers that would otherwise be configured by default .
* /
public void setArgumentResolvers ( List < HandlerMethodArgumentResolver > resolvers ) {
this . argumentResolvers . clear ( ) ;
this . argumentResolvers . addAll ( resolvers ) ;
this . argumentResolvers = new ArrayList < > ( resolvers ) ;
}
/ * *
@ -91,22 +108,35 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, BeanFactory
@@ -91,22 +108,35 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, BeanFactory
return this . argumentResolvers ;
}
/ * *
* Configure message converters to read the request body with .
* /
public void setMessageConverters ( List < HttpMessageConverter < ? > > messageConverters ) {
this . messageConverters . clear ( ) ;
this . messageConverters . addAll ( messageConverters ) ;
}
/ * *
* Provide the message converters to use for argument resolution .
* Return the configured message converters .
* /
public List < HttpMessageConverter < ? > > getMessageConverters ( ) {
return this . messageConverters ;
}
/ * *
* Configure a ConversionService for type conversion of controller method
* arguments as well as for converting from different async types to
* { @code Flux } and { @code Mono } .
*
* TODO : this may be replaced by DataBinder
* /
public void setConversionService ( ConversionService conversionService ) {
this . conversionService = conversionService ;
}
/ * *
* Return the configured ConversionService .
* /
public ConversionService getConversionService ( ) {
return this . conversionService ;
}
@ -123,41 +153,45 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, BeanFactory
@@ -123,41 +153,45 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, BeanFactory
}
public ConfigurableBeanFactory getBeanFactory ( ) {
return beanFactory ;
return this . beanFactory ;
}
@Override
public void afterPropertiesSet ( ) throws Exception {
if ( ObjectUtils . isEmpty ( this . argumentResolvers ) ) {
if ( this . argumentResolvers = = null ) {
this . argumentResolvers = initArgumentResolvers ( ) ;
}
}
// List<HttpMessageConverter<?>> converters = Arrays.asList(
// new CodecHttpMessageConverter<ByteBuffer>(new ByteBufferEncoder(), new ByteBufferDecoder()),
// new CodecHttpMessageConverter<String>(new StringEncoder(), new StringDecoder()),
// new CodecHttpMessageConverter<Object>(new Jaxb2Encoder(), new Jaxb2Decoder()),
// new CodecHttpMessageConverter<Object>(new JacksonJsonEncoder(),
// new JacksonJsonDecoder(new JsonObjectDecoder())));
protected List < HandlerMethodArgumentResolver > initArgumentResolvers ( ) {
List < HandlerMethodArgumentResolver > resolvers = new ArrayList < > ( ) ;
// Annotation-based argument resolution
ConversionService cs = getConversionService ( ) ;
this . a rgumentR esolvers . add ( new RequestParamMethodArgumentResolver ( cs , getBeanFactory ( ) , false ) ) ;
this . a rgumentR esolvers . add ( new RequestParamMapMethodArgumentResolver ( ) ) ;
this . a rgumentR esolvers . add ( new PathVariableMethodArgumentResolver ( cs , getBeanFactory ( ) ) ) ;
this . a rgumentR esolvers . add ( new PathVariableMapMethodArgumentResolver ( ) ) ;
this . a rgumentR esolvers . add ( new RequestBodyArgumentResolver ( getMessageConverters ( ) , cs ) ) ;
this . a rgumentR esolvers . add ( new RequestHeaderMethodArgumentResolver ( cs , getBeanFactory ( ) ) ) ;
this . a rgumentR esolvers . add ( new RequestHeaderMapMethodArgumentResolver ( ) ) ;
this . a rgumentR esolvers . add ( new CookieValueMethodArgumentResolver ( cs , getBeanFactory ( ) ) ) ;
this . a rgumentR esolvers . add ( new ExpressionValueMethodArgumentResolver ( cs , getBeanFactory ( ) ) ) ;
this . a rgumentR esolvers . add ( new SessionAttributeMethodArgumentResolver ( cs , getBeanFactory ( ) ) ) ;
this . a rgumentR esolvers . add ( new RequestAttributeMethodArgumentResolver ( cs , getBeanFactory ( ) ) ) ;
resolvers . add ( new RequestParamMethodArgumentResolver ( cs , getBeanFactory ( ) , false ) ) ;
resolvers . add ( new RequestParamMapMethodArgumentResolver ( ) ) ;
resolvers . add ( new PathVariableMethodArgumentResolver ( cs , getBeanFactory ( ) ) ) ;
resolvers . add ( new PathVariableMapMethodArgumentResolver ( ) ) ;
resolvers . add ( new RequestBodyArgumentResolver ( getMessageConverters ( ) , cs ) ) ;
resolvers . add ( new RequestHeaderMethodArgumentResolver ( cs , getBeanFactory ( ) ) ) ;
resolvers . add ( new RequestHeaderMapMethodArgumentResolver ( ) ) ;
resolvers . add ( new CookieValueMethodArgumentResolver ( cs , getBeanFactory ( ) ) ) ;
resolvers . add ( new ExpressionValueMethodArgumentResolver ( cs , getBeanFactory ( ) ) ) ;
resolvers . add ( new SessionAttributeMethodArgumentResolver ( cs , getBeanFactory ( ) ) ) ;
resolvers . add ( new RequestAttributeMethodArgumentResolver ( cs , getBeanFactory ( ) ) ) ;
// Type-based argument resolution
this . a rgumentR esolvers . add ( new ModelArgumentResolver ( ) ) ;
resolvers . add ( new ModelArgumentResolver ( ) ) ;
// Catch-all
this . argumentResolvers . add ( new RequestParamMethodArgumentResolver ( cs , getBeanFactory ( ) , true ) ) ;
// Custom resolvers
if ( getCustomArgumentResolvers ( ) ! = null ) {
resolvers . addAll ( getCustomArgumentResolvers ( ) ) ;
}
// Catch-all
resolvers . add ( new RequestParamMethodArgumentResolver ( cs , getBeanFactory ( ) , true ) ) ;
return resolvers ;
}
@Override
@ -169,7 +203,7 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, BeanFactory
@@ -169,7 +203,7 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, BeanFactory
public Mono < HandlerResult > handle ( ServerWebExchange exchange , Object handler ) {
HandlerMethod handlerMethod = ( HandlerMethod ) handler ;
InvocableHandlerMethod invocable = new InvocableHandlerMethod ( handlerMethod ) ;
invocable . setHandlerMethodArgumentResolvers ( this . argumentResolvers ) ;
invocable . setHandlerMethodArgumentResolvers ( getArgumentResolvers ( ) ) ;
ModelMap model = new ExtendedModelMap ( ) ;
return invocable . invokeForRequest ( exchange , model )
. map ( result - > result . setExceptionHandler ( ex - > handleException ( ex , handlerMethod , exchange ) ) )