Java Code Examples for org.springframework.web.method.support.HandlerMethodArgumentResolver

The following examples show how to use org.springframework.web.method.support.HandlerMethodArgumentResolver. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Return the list of argument resolvers to use including built-in resolvers
 * and custom resolvers provided via {@link #setCustomArgumentResolvers}.
 */
protected List<HandlerMethodArgumentResolver> getDefaultArgumentResolvers() {
	List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();

	// Annotation-based argument resolution
	resolvers.add(new SessionAttributeMethodArgumentResolver());
	resolvers.add(new RequestAttributeMethodArgumentResolver());

	// Type-based argument resolution
	resolvers.add(new ServletRequestMethodArgumentResolver());
	resolvers.add(new ServletResponseMethodArgumentResolver());
	resolvers.add(new RedirectAttributesMethodArgumentResolver());
	resolvers.add(new ModelMethodProcessor());

	// Custom arguments
	if (getCustomArgumentResolvers() != null) {
		resolvers.addAll(getCustomArgumentResolvers());
	}

	return resolvers;
}
 
Example 2
@Before
public void setup() throws Exception {
	ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
	bindingInitializer.setValidator(new StubValidator());

	List<HandlerMethodArgumentResolver> customResolvers = new ArrayList<>();
	customResolvers.add(new ServletWebArgumentResolverAdapter(new ColorArgumentResolver()));

	GenericWebApplicationContext context = new GenericWebApplicationContext();
	context.refresh();

	handlerAdapter = new RequestMappingHandlerAdapter();
	handlerAdapter.setWebBindingInitializer(bindingInitializer);
	handlerAdapter.setCustomArgumentResolvers(customResolvers);
	handlerAdapter.setApplicationContext(context);
	handlerAdapter.setBeanFactory(context.getBeanFactory());
	handlerAdapter.afterPropertiesSet();

	request = new MockHttpServletRequest();
	response = new MockHttpServletResponse();

	request.setMethod("POST");

	// Expose request to the current thread (for SpEL expressions)
	RequestContextHolder.setRequestAttributes(new ServletWebRequest(request));
}
 
Example 3
@Test
public void setAlwaysUseRedirectAttributes() throws Exception {
	HandlerMethodArgumentResolver redirectAttributesResolver = new RedirectAttributesMethodArgumentResolver();
	HandlerMethodArgumentResolver modelResolver = new ModelMethodProcessor();
	HandlerMethodReturnValueHandler viewHandler = new ViewNameMethodReturnValueHandler();

	this.handlerAdapter.setArgumentResolvers(Arrays.asList(redirectAttributesResolver, modelResolver));
	this.handlerAdapter.setReturnValueHandlers(Collections.singletonList(viewHandler));
	this.handlerAdapter.setIgnoreDefaultModelOnRedirect(true);
	this.handlerAdapter.afterPropertiesSet();

	this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());

	HandlerMethod handlerMethod = handlerMethod(new RedirectAttributeController(), "handle", Model.class);
	ModelAndView mav = this.handlerAdapter.handle(request, response, handlerMethod);

	assertTrue("Without RedirectAttributes arg, model should be empty", mav.getModel().isEmpty());
}
 
Example 4
/**
 * Return the list of argument resolvers to use including built-in resolvers
 * and custom resolvers provided via {@link #setCustomArgumentResolvers}.
 */
protected List<HandlerMethodArgumentResolver> getDefaultArgumentResolvers() {
	List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();

	// Annotation-based argument resolution
	resolvers.add(new SessionAttributeMethodArgumentResolver());
	resolvers.add(new RequestAttributeMethodArgumentResolver());

	// Type-based argument resolution
	resolvers.add(new ServletRequestMethodArgumentResolver());
	resolvers.add(new ServletResponseMethodArgumentResolver());
	resolvers.add(new RedirectAttributesMethodArgumentResolver());
	resolvers.add(new ModelMethodProcessor());

	// Custom arguments
	if (getCustomArgumentResolvers() != null) {
		resolvers.addAll(getCustomArgumentResolvers());
	}

	return resolvers;
}
 
Example 5
@Before
public void setup() throws Exception {
	ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
	bindingInitializer.setValidator(new StubValidator());

	List<HandlerMethodArgumentResolver> customResolvers = new ArrayList<>();
	customResolvers.add(new ServletWebArgumentResolverAdapter(new ColorArgumentResolver()));

	GenericWebApplicationContext context = new GenericWebApplicationContext();
	context.refresh();

	handlerAdapter = new RequestMappingHandlerAdapter();
	handlerAdapter.setWebBindingInitializer(bindingInitializer);
	handlerAdapter.setCustomArgumentResolvers(customResolvers);
	handlerAdapter.setApplicationContext(context);
	handlerAdapter.setBeanFactory(context.getBeanFactory());
	handlerAdapter.afterPropertiesSet();

	request = new MockHttpServletRequest();
	response = new MockHttpServletResponse();

	request.setMethod("POST");

	// Expose request to the current thread (for SpEL expressions)
	RequestContextHolder.setRequestAttributes(new ServletWebRequest(request));
}
 
Example 6
@Test
public void setAlwaysUseRedirectAttributes() throws Exception {
	HandlerMethodArgumentResolver redirectAttributesResolver = new RedirectAttributesMethodArgumentResolver();
	HandlerMethodArgumentResolver modelResolver = new ModelMethodProcessor();
	HandlerMethodReturnValueHandler viewHandler = new ViewNameMethodReturnValueHandler();

	this.handlerAdapter.setArgumentResolvers(Arrays.asList(redirectAttributesResolver, modelResolver));
	this.handlerAdapter.setReturnValueHandlers(Collections.singletonList(viewHandler));
	this.handlerAdapter.setIgnoreDefaultModelOnRedirect(true);
	this.handlerAdapter.afterPropertiesSet();

	this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());

	HandlerMethod handlerMethod = handlerMethod(new RedirectAttributeController(), "handle", Model.class);
	ModelAndView mav = this.handlerAdapter.handle(request, response, handlerMethod);

	assertTrue("Without RedirectAttributes arg, model should be empty", mav.getModel().isEmpty());
}
 
Example 7
/**
 * Configure the complete list of supported argument types thus overriding
 * the resolvers that would otherwise be configured by default.
 */
public void setArgumentResolvers(@Nullable List<HandlerMethodArgumentResolver> argumentResolvers) {
	if (argumentResolvers == null) {
		this.argumentResolvers = null;
	}
	else {
		this.argumentResolvers = new HandlerMethodArgumentResolverComposite();
		this.argumentResolvers.addResolvers(argumentResolvers);
	}
}
 
Example 8
@Override
public void afterPropertiesSet() {
	// Do this first, it may add ResponseBodyAdvice beans
	initExceptionHandlerAdviceCache();

	if (this.argumentResolvers == null) {
		List<HandlerMethodArgumentResolver> resolvers = getDefaultArgumentResolvers();
		this.argumentResolvers = new HandlerMethodArgumentResolverComposite().addResolvers(resolvers);
	}
	if (this.returnValueHandlers == null) {
		List<HandlerMethodReturnValueHandler> handlers = getDefaultReturnValueHandlers();
		this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite().addHandlers(handlers);
	}
}
 
Example 9
/**
 * Configure the complete list of supported argument types thus overriding
 * the resolvers that would otherwise be configured by default.
 */
public void setArgumentResolvers(@Nullable List<HandlerMethodArgumentResolver> argumentResolvers) {
	if (argumentResolvers == null) {
		this.argumentResolvers = null;
	}
	else {
		this.argumentResolvers = new HandlerMethodArgumentResolverComposite();
		this.argumentResolvers.addResolvers(argumentResolvers);
	}
}
 
Example 10
/**
 * Configure the supported argument types in {@code @InitBinder} methods.
 */
public void setInitBinderArgumentResolvers(@Nullable List<HandlerMethodArgumentResolver> argumentResolvers) {
	if (argumentResolvers == null) {
		this.initBinderArgumentResolvers = null;
	}
	else {
		this.initBinderArgumentResolvers = new HandlerMethodArgumentResolverComposite();
		this.initBinderArgumentResolvers.addResolvers(argumentResolvers);
	}
}
 
Example 11
/**
 * Return the list of argument resolvers to use for {@code @InitBinder}
 * methods including built-in and custom resolvers.
 */
private List<HandlerMethodArgumentResolver> getDefaultInitBinderArgumentResolvers() {
	List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();

	// Annotation-based argument resolution
	resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(), false));
	resolvers.add(new RequestParamMapMethodArgumentResolver());
	resolvers.add(new PathVariableMethodArgumentResolver());
	resolvers.add(new PathVariableMapMethodArgumentResolver());
	resolvers.add(new MatrixVariableMethodArgumentResolver());
	resolvers.add(new MatrixVariableMapMethodArgumentResolver());
	resolvers.add(new ExpressionValueMethodArgumentResolver(getBeanFactory()));
	resolvers.add(new SessionAttributeMethodArgumentResolver());
	resolvers.add(new RequestAttributeMethodArgumentResolver());

	// Type-based argument resolution
	resolvers.add(new ServletRequestMethodArgumentResolver());
	resolvers.add(new ServletResponseMethodArgumentResolver());

	// Custom arguments
	if (getCustomArgumentResolvers() != null) {
		resolvers.addAll(getCustomArgumentResolvers());
	}

	// Catch-all
	resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(), true));

	return resolvers;
}
 
Example 12
Source Project: spring-analysis-note   Source File: WebMvcConfigurationSupport.java    License: MIT License 5 votes vote down vote up
/**
 * Provide access to the shared custom argument resolvers used by the
 * {@link RequestMappingHandlerAdapter} and the {@link ExceptionHandlerExceptionResolver}.
 * <p>This method cannot be overridden; use {@link #addArgumentResolvers} instead.
 * @since 4.3
 */
protected final List<HandlerMethodArgumentResolver> getArgumentResolvers() {
	if (this.argumentResolvers == null) {
		this.argumentResolvers = new ArrayList<>();
		addArgumentResolvers(this.argumentResolvers);
	}
	return this.argumentResolvers;
}
 
Example 13
@Test
public void setCustomArgumentResolvers() {
	HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver();
	this.resolver.setCustomArgumentResolvers(Collections.singletonList(resolver));
	this.resolver.afterPropertiesSet();

	assertTrue(this.resolver.getArgumentResolvers().getResolvers().contains(resolver));
	assertMethodProcessorCount(RESOLVER_COUNT + 1, HANDLER_COUNT);
}
 
Example 14
@Test
public void setArgumentResolvers() {
	HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver();
	this.resolver.setArgumentResolvers(Collections.singletonList(resolver));
	this.resolver.afterPropertiesSet();

	assertMethodProcessorCount(1, HANDLER_COUNT);
}
 
Example 15
@Test
public void setCustomArgumentResolvers() throws Exception {
	HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver();
	this.handlerAdapter.setCustomArgumentResolvers(Collections.singletonList(resolver));
	this.handlerAdapter.afterPropertiesSet();

	assertTrue(this.handlerAdapter.getArgumentResolvers().contains(resolver));
	assertMethodProcessorCount(RESOLVER_COUNT + 1, INIT_BINDER_RESOLVER_COUNT + 1, HANDLER_COUNT);
}
 
Example 16
@Test
public void setArgumentResolvers() throws Exception {
	HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver();
	this.handlerAdapter.setArgumentResolvers(Collections.singletonList(resolver));
	this.handlerAdapter.afterPropertiesSet();

	assertMethodProcessorCount(1, INIT_BINDER_RESOLVER_COUNT, HANDLER_COUNT);
}
 
Example 17
@Test
public void setInitBinderArgumentResolvers() throws Exception {
	HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver();
	this.handlerAdapter.setInitBinderArgumentResolvers(Collections.singletonList(resolver));
	this.handlerAdapter.afterPropertiesSet();

	assertMethodProcessorCount(RESOLVER_COUNT, 1, HANDLER_COUNT);
}
 
Example 18
private void testArgumentResolvers(Object bean) {
	assertNotNull(bean);
	Object value = new DirectFieldAccessor(bean).getPropertyValue("customArgumentResolvers");
	assertNotNull(value);
	assertTrue(value instanceof List);
	@SuppressWarnings("unchecked")
	List<HandlerMethodArgumentResolver> resolvers = (List<HandlerMethodArgumentResolver>) value;
	assertEquals(3, resolvers.size());
	assertTrue(resolvers.get(0) instanceof ServletWebArgumentResolverAdapter);
	assertTrue(resolvers.get(1) instanceof TestHandlerMethodArgumentResolver);
	assertTrue(resolvers.get(2) instanceof TestHandlerMethodArgumentResolver);
	assertNotSame(resolvers.get(1), resolvers.get(2));
}
 
Example 19
Source Project: Milkomeda   Source File: CometConfig.java    License: MIT License 5 votes vote down vote up
@Autowired
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public void configParamResolve(RequestMappingHandlerAdapter adapter) {
    List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
    // 动态添加针对注解 @CometParam 处理的解析器
    argumentResolvers.add(new CometParamResolver());
    argumentResolvers.addAll(Objects.requireNonNull(adapter.getArgumentResolvers()));
    adapter.setArgumentResolvers(argumentResolvers);
}
 
Example 20
/**
 * Token参数解析
 *
 * @param argumentResolvers 解析类
 */
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
	//注入用户信息
	argumentResolvers.add(new TokenArgumentResolver(userService));
	//注入应用信息
	argumentResolvers.add(new ClientArgumentResolver());
}
 
Example 21
Source Project: yue-library   Source File: CustomArgumentResolversConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Autowired
public void prioritizeCustomMethodArgumentHandlers(RequestMappingHandlerAdapter adapter) {
	List<HandlerMethodArgumentResolver> prioritizeCustomArgumentResolvers = new ArrayList<>();
	prioritizeCustomArgumentResolvers.add(new JSONObjectArgumentResolver());
	prioritizeCustomArgumentResolvers.addAll(adapter.getArgumentResolvers());
	adapter.setArgumentResolvers(prioritizeCustomArgumentResolvers);
}
 
Example 22
/**
 * Configure the complete list of supported argument types thus overriding
 * the resolvers that would otherwise be configured by default.
 */
public void setArgumentResolvers(@Nullable List<HandlerMethodArgumentResolver> argumentResolvers) {
	if (argumentResolvers == null) {
		this.argumentResolvers = null;
	}
	else {
		this.argumentResolvers = new HandlerMethodArgumentResolverComposite();
		this.argumentResolvers.addResolvers(argumentResolvers);
	}
}
 
Example 23
@Override
public void afterPropertiesSet() {
	// Do this first, it may add ResponseBodyAdvice beans
	initExceptionHandlerAdviceCache();

	if (this.argumentResolvers == null) {
		List<HandlerMethodArgumentResolver> resolvers = getDefaultArgumentResolvers();
		this.argumentResolvers = new HandlerMethodArgumentResolverComposite().addResolvers(resolvers);
	}
	if (this.returnValueHandlers == null) {
		List<HandlerMethodReturnValueHandler> handlers = getDefaultReturnValueHandlers();
		this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite().addHandlers(handlers);
	}
}
 
Example 24
/**
 * Configure the complete list of supported argument types thus overriding
 * the resolvers that would otherwise be configured by default.
 */
public void setArgumentResolvers(@Nullable List<HandlerMethodArgumentResolver> argumentResolvers) {
	if (argumentResolvers == null) {
		this.argumentResolvers = null;
	}
	else {
		this.argumentResolvers = new HandlerMethodArgumentResolverComposite();
		this.argumentResolvers.addResolvers(argumentResolvers);
	}
}
 
Example 25
/**
 * Configure the supported argument types in {@code @InitBinder} methods.
 */
public void setInitBinderArgumentResolvers(@Nullable List<HandlerMethodArgumentResolver> argumentResolvers) {
	if (argumentResolvers == null) {
		this.initBinderArgumentResolvers = null;
	}
	else {
		this.initBinderArgumentResolvers = new HandlerMethodArgumentResolverComposite();
		this.initBinderArgumentResolvers.addResolvers(argumentResolvers);
	}
}
 
Example 26
/**
 * Return the list of argument resolvers to use for {@code @InitBinder}
 * methods including built-in and custom resolvers.
 */
private List<HandlerMethodArgumentResolver> getDefaultInitBinderArgumentResolvers() {
	List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();

	// Annotation-based argument resolution
	resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(), false));
	resolvers.add(new RequestParamMapMethodArgumentResolver());
	resolvers.add(new PathVariableMethodArgumentResolver());
	resolvers.add(new PathVariableMapMethodArgumentResolver());
	resolvers.add(new MatrixVariableMethodArgumentResolver());
	resolvers.add(new MatrixVariableMapMethodArgumentResolver());
	resolvers.add(new ExpressionValueMethodArgumentResolver(getBeanFactory()));
	resolvers.add(new SessionAttributeMethodArgumentResolver());
	resolvers.add(new RequestAttributeMethodArgumentResolver());

	// Type-based argument resolution
	resolvers.add(new ServletRequestMethodArgumentResolver());
	resolvers.add(new ServletResponseMethodArgumentResolver());

	// Custom arguments
	if (getCustomArgumentResolvers() != null) {
		resolvers.addAll(getCustomArgumentResolvers());
	}

	// Catch-all
	resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(), true));

	return resolvers;
}
 
Example 27
Source Project: java-technology-stack   Source File: WebMvcConfigurationSupport.java    License: MIT License 5 votes vote down vote up
/**
 * Provide access to the shared custom argument resolvers used by the
 * {@link RequestMappingHandlerAdapter} and the {@link ExceptionHandlerExceptionResolver}.
 * <p>This method cannot be overridden; use {@link #addArgumentResolvers} instead.
 * @since 4.3
 */
protected final List<HandlerMethodArgumentResolver> getArgumentResolvers() {
	if (this.argumentResolvers == null) {
		this.argumentResolvers = new ArrayList<>();
		addArgumentResolvers(this.argumentResolvers);
	}
	return this.argumentResolvers;
}
 
Example 28
@Test
public void setArgumentResolvers() {
	HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver();
	this.resolver.setArgumentResolvers(Collections.singletonList(resolver));
	this.resolver.afterPropertiesSet();

	assertMethodProcessorCount(1, HANDLER_COUNT);
}
 
Example 29
@Test
public void setCustomArgumentResolvers() throws Exception {
	HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver();
	this.handlerAdapter.setCustomArgumentResolvers(Collections.singletonList(resolver));
	this.handlerAdapter.afterPropertiesSet();

	assertTrue(this.handlerAdapter.getArgumentResolvers().contains(resolver));
	assertMethodProcessorCount(RESOLVER_COUNT + 1, INIT_BINDER_RESOLVER_COUNT + 1, HANDLER_COUNT);
}
 
Example 30
@Test
public void setArgumentResolvers() throws Exception {
	HandlerMethodArgumentResolver resolver = new ServletRequestMethodArgumentResolver();
	this.handlerAdapter.setArgumentResolvers(Collections.singletonList(resolver));
	this.handlerAdapter.afterPropertiesSet();

	assertMethodProcessorCount(1, INIT_BINDER_RESOLVER_COUNT, HANDLER_COUNT);
}