resolvers.add(new MatrixVariableMethodArgumentResolver()); resolvers.add(new MatrixVariableMapMethodArgumentResolver()); resolvers.add(new ServletModelAttributeMethodProcessor(false)); resolvers.add(new RequestResponseBodyMethodProcessor(getMessageConverters(), this.requestResponseBodyAdvice)); resolvers.add(new RequestPartMethodArgumentResolver(getMessageConverters(), this.requestResponseBodyAdvice)); resolvers.add(new ServletModelAttributeMethodProcessor(true));
/** * Instantiate the model attribute from a URI template variable or from a * request parameter if the name matches to the model attribute name and * if there is an appropriate type conversion strategy. If none of these * are true delegate back to the base class. * @see #createAttributeFromRequestValue */ @Override protected final Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { String value = getRequestValueForAttribute(attributeName, request); if (value != null) { Object attribute = createAttributeFromRequestValue( value, attributeName, parameter, binderFactory, request); if (attribute != null) { return attribute; } } return super.createAttribute(attributeName, parameter, binderFactory, request); }
/** * Obtain a value from the request that may be used to instantiate the * model attribute through type conversion from String to the target type. * <p>The default implementation looks for the attribute name to match * a URI variable first and then a request parameter. * @param attributeName the model attribute name * @param request the current request * @return the request value to try to convert, or {@code null} if none */ @Nullable protected String getRequestValueForAttribute(String attributeName, NativeWebRequest request) { Map<String, String> variables = getUriTemplateVariables(request); String variableValue = variables.get(attributeName); if (StringUtils.hasText(variableValue)) { return variableValue; } String parameterValue = request.getParameter(attributeName); if (StringUtils.hasText(parameterValue)) { return parameterValue; } return null; }
@Test public void createAttributeUriTemplateVarCannotConvert() throws Exception { Map<String, String> uriTemplateVars = new HashMap<>(); uriTemplateVars.put("testBean2", "Patty"); request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars); TestBeanWithoutStringConstructor testBean = (TestBeanWithoutStringConstructor) processor.resolveArgument( testBeanWithoutStringConstructorModelAttr, mavContainer, webRequest, binderFactory); assertNotNull(testBean); }
@Test @SuppressWarnings("unchecked") public void createAttributeUriTemplateVarWithOptional() throws Exception { Map<String, String> uriTemplateVars = new HashMap<>(); uriTemplateVars.put("testBean3", "Patty"); request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars); // Type conversion from "Patty" to TestBean via TestBean(String) constructor Optional<TestBean> testBean = (Optional<TestBean>) processor.resolveArgument( testBeanWithOptionalModelAttr, mavContainer, webRequest, binderFactory); assertEquals("Patty", testBean.get().getName()); }
resolvers.add(new RequestParamMapMethodArgumentResolver()); resolvers.add(new PathVariableMethodArgumentResolver()); resolvers.add(new ServletModelAttributeMethodProcessor(false)); resolvers.add(new RequestResponseBodyMethodProcessor(getMessageConverters())); resolvers.add(new RequestPartMethodArgumentResolver(getMessageConverters())); resolvers.add(new ServletModelAttributeMethodProcessor(true));
/** * Instantiate the model attribute from a URI template variable or from a * request parameter if the name matches to the model attribute name and * if there is an appropriate type conversion strategy. If none of these * are true delegate back to the base class. * @see #createAttributeFromRequestValue */ @Override protected final Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { String value = getRequestValueForAttribute(attributeName, request); if (value != null) { Object attribute = createAttributeFromRequestValue( value, attributeName, parameter, binderFactory, request); if (attribute != null) { return attribute; } } return super.createAttribute(attributeName, parameter, binderFactory, request); }
/** * Obtain a value from the request that may be used to instantiate the * model attribute through type conversion from String to the target type. * <p>The default implementation looks for the attribute name to match * a URI variable first and then a request parameter. * @param attributeName the model attribute name * @param request the current request * @return the request value to try to convert, or {@code null} if none */ @Nullable protected String getRequestValueForAttribute(String attributeName, NativeWebRequest request) { Map<String, String> variables = getUriTemplateVariables(request); String variableValue = variables.get(attributeName); if (StringUtils.hasText(variableValue)) { return variableValue; } String parameterValue = request.getParameter(attributeName); if (StringUtils.hasText(parameterValue)) { return parameterValue; } return null; }
@Test public void createAttributeUriTemplateVar() throws Exception { Map<String, String> uriTemplateVars = new HashMap<>(); uriTemplateVars.put("testBean1", "Patty"); request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars); // Type conversion from "Patty" to TestBean via TestBean(String) constructor TestBean testBean = (TestBean) processor.resolveArgument( testBeanModelAttr, mavContainer, webRequest, binderFactory); assertEquals("Patty", testBean.getName()); }
resolvers.add(new MatrixVariableMethodArgumentResolver()); resolvers.add(new MatrixVariableMapMethodArgumentResolver()); resolvers.add(new ServletModelAttributeMethodProcessor(false)); resolvers.add(new RequestResponseBodyMethodProcessor(getMessageConverters(), this.requestResponseBodyAdvice)); resolvers.add(new RequestPartMethodArgumentResolver(getMessageConverters(), this.requestResponseBodyAdvice)); resolvers.add(new ServletModelAttributeMethodProcessor(true));
/** * Instantiate the model attribute from a URI template variable or from a * request parameter if the name matches to the model attribute name and * if there is an appropriate type conversion strategy. If none of these * are true delegate back to the base class. * @see #createAttributeFromRequestValue */ @Override protected final Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { String value = getRequestValueForAttribute(attributeName, request); if (value != null) { Object attribute = createAttributeFromRequestValue( value, attributeName, parameter, binderFactory, request); if (attribute != null) { return attribute; } } return super.createAttribute(attributeName, parameter, binderFactory, request); }
/** * Obtain a value from the request that may be used to instantiate the * model attribute through type conversion from String to the target type. * <p>The default implementation looks for the attribute name to match * a URI variable first and then a request parameter. * @param attributeName the model attribute name * @param request the current request * @return the request value to try to convert, or {@code null} if none */ protected String getRequestValueForAttribute(String attributeName, NativeWebRequest request) { Map<String, String> variables = getUriTemplateVariables(request); String variableValue = variables.get(attributeName); if (StringUtils.hasText(variableValue)) { return variableValue; } String parameterValue = request.getParameter(attributeName); if (StringUtils.hasText(parameterValue)) { return parameterValue; } return null; }
@Test @SuppressWarnings("unchecked") public void createAttributeRequestParameterWithOptional() throws Exception { request.addParameter("testBean3", "Patty"); Optional<TestBean> testBean = (Optional<TestBean>) processor.resolveArgument( testBeanWithOptionalModelAttr, mavContainer, webRequest, binderFactory); assertEquals("Patty", testBean.get().getName()); }
@Before public void setup() throws Exception { processor = new ServletModelAttributeMethodProcessor(false); ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer(); initializer.setConversionService(new DefaultConversionService()); binderFactory = new ServletRequestDataBinderFactory(null, initializer); mavContainer = new ModelAndViewContainer(); request = new MockHttpServletRequest(); webRequest = new ServletWebRequest(request); Method method = getClass().getDeclaredMethod("modelAttribute", TestBean.class, TestBeanWithoutStringConstructor.class, Optional.class); testBeanModelAttr = new MethodParameter(method, 0); testBeanWithoutStringConstructorModelAttr = new MethodParameter(method, 1); testBeanWithOptionalModelAttr = new MethodParameter(method, 2); }
@Test public void createAttributeRequestParameterCannotConvert() throws Exception { request.addParameter("testBean2", "Patty"); TestBeanWithoutStringConstructor testBean = (TestBeanWithoutStringConstructor) processor.resolveArgument( testBeanWithoutStringConstructorModelAttr, mavContainer, webRequest, binderFactory); assertNotNull(testBean); }
private List<HandlerMethodArgumentResolver> getArgumentResolvers(List<HttpMessageConverter<?>> messageConverters) { List<HandlerMethodArgumentResolver> resolvers = new LinkedList<>(); resolvers.add(new RequestParamMethodArgumentResolver(beanFactory, false)); resolvers.add(new RequestParamMapMethodArgumentResolver()); resolvers.add(new MatrixVariableMethodArgumentResolver()); resolvers.add(new MatrixVariableMapMethodArgumentResolver()); resolvers.add(new ServletModelAttributeMethodProcessor(false)); resolvers.add(new RequestResponseBodyMethodProcessor(messageConverters)); resolvers.add(new RequestPartMethodArgumentResolver(messageConverters)); resolvers.add(new RequestHeaderMethodArgumentResolver(beanFactory)); resolvers.add(new RequestHeaderMapMethodArgumentResolver()); resolvers.add(new ServletCookieValueMethodArgumentResolver(beanFactory)); resolvers.add(new ExpressionValueMethodArgumentResolver(beanFactory)); resolvers.add(new ServletRequestMethodArgumentResolver()); resolvers.add(new ServletResponseMethodArgumentResolver()); resolvers.add(new HttpEntityMethodProcessor(messageConverters)); resolvers.add(new RedirectAttributesMethodArgumentResolver()); resolvers.add(new ModelMethodProcessor()); resolvers.add(new MapMethodProcessor()); resolvers.add(new ErrorsMethodArgumentResolver()); resolvers.add(new SessionStatusMethodArgumentResolver()); resolvers.add(new UriComponentsBuilderMethodArgumentResolver()); resolvers.add(new RequestParamMethodArgumentResolver(beanFactory, true)); resolvers.add(new ServletModelAttributeMethodProcessor(true)); return resolvers; }
@Test @SuppressWarnings("unchecked") public void attributesAsOptionalEmpty() throws Exception { request.addParameter("name", "Patty"); mavContainer.getModel().put("testBean1", Optional.empty()); mavContainer.getModel().put("testBean2", Optional.empty()); mavContainer.getModel().put("testBean3", Optional.empty()); assertNull(processor.resolveArgument( testBeanModelAttr, mavContainer, webRequest, binderFactory)); assertNull(processor.resolveArgument( testBeanWithoutStringConstructorModelAttr, mavContainer, webRequest, binderFactory)); Optional<TestBean> testBean =(Optional<TestBean>) processor.resolveArgument( testBeanWithOptionalModelAttr, mavContainer, webRequest, binderFactory); assertFalse(testBean.isPresent()); }
resolvers.add(new MatrixVariableMethodArgumentResolver()); resolvers.add(new MatrixVariableMapMethodArgumentResolver()); resolvers.add(new ServletModelAttributeMethodProcessor(false)); resolvers.add(new RequestResponseBodyMethodProcessor(getMessageConverters())); resolvers.add(new RequestPartMethodArgumentResolver(getMessageConverters())); resolvers.add(new ServletModelAttributeMethodProcessor(true));
@Test @SuppressWarnings("unchecked") public void attributesAsNullValues() throws Exception { request.addParameter("name", "Patty"); mavContainer.getModel().put("testBean1", null); mavContainer.getModel().put("testBean2", null); mavContainer.getModel().put("testBean3", null); assertNull(processor.resolveArgument( testBeanModelAttr, mavContainer, webRequest, binderFactory)); assertNull(processor.resolveArgument( testBeanWithoutStringConstructorModelAttr, mavContainer, webRequest, binderFactory)); Optional<TestBean> testBean = (Optional<TestBean>) processor.resolveArgument( testBeanWithOptionalModelAttr, mavContainer, webRequest, binderFactory); assertFalse(testBean.isPresent()); }
resolvers.add(new MatrixVariableMethodArgumentResolver()); resolvers.add(new MatrixVariableMapMethodArgumentResolver()); resolvers.add(new ServletModelAttributeMethodProcessor(false)); resolvers.add(new RequestResponseBodyMethodProcessor(getMessageConverters(), this.requestResponseBodyAdvice)); resolvers.add(new ServletModelAttributeMethodProcessor(true));