001/* 002 * Copyright 2002-2020 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.method.annotation; 018 019import java.util.Map; 020 021import org.springframework.core.MethodParameter; 022import org.springframework.lang.Nullable; 023import org.springframework.util.Assert; 024import org.springframework.web.bind.support.WebDataBinderFactory; 025import org.springframework.web.context.request.NativeWebRequest; 026import org.springframework.web.method.support.HandlerMethodArgumentResolver; 027import org.springframework.web.method.support.HandlerMethodReturnValueHandler; 028import org.springframework.web.method.support.ModelAndViewContainer; 029 030/** 031 * Resolves {@link Map} method arguments and handles {@link Map} return values. 032 * 033 * <p>A Map return value can be interpreted in more than one ways depending 034 * on the presence of annotations like {@code @ModelAttribute} or 035 * {@code @ResponseBody}. As of 5.2 this resolver returns false if the 036 * parameter is annotated. 037 * 038 * @author Rossen Stoyanchev 039 * @since 3.1 040 */ 041public class MapMethodProcessor implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler { 042 043 @Override 044 public boolean supportsParameter(MethodParameter parameter) { 045 return (Map.class.isAssignableFrom(parameter.getParameterType()) && 046 parameter.getParameterAnnotations().length == 0); 047 } 048 049 @Override 050 @Nullable 051 public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, 052 NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception { 053 054 Assert.state(mavContainer != null, "ModelAndViewContainer is required for model exposure"); 055 return mavContainer.getModel(); 056 } 057 058 @Override 059 public boolean supportsReturnType(MethodParameter returnType) { 060 return Map.class.isAssignableFrom(returnType.getParameterType()); 061 } 062 063 @Override 064 @SuppressWarnings({"rawtypes", "unchecked"}) 065 public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, 066 ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { 067 068 if (returnValue instanceof Map){ 069 mavContainer.addAllAttributes((Map) returnValue); 070 } 071 else if (returnValue != null) { 072 // should not happen 073 throw new UnsupportedOperationException("Unexpected return type [" + 074 returnType.getParameterType().getName() + "] in method: " + returnType.getMethod()); 075 } 076 } 077 078}