001/* 002 * Copyright 2002-2017 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.bind.support; 018 019import org.springframework.core.MethodParameter; 020import org.springframework.lang.Nullable; 021import org.springframework.web.context.request.NativeWebRequest; 022 023/** 024 * SPI for resolving custom arguments for a specific handler method parameter. 025 * Typically implemented to detect special parameter types, resolving 026 * well-known argument values for them. 027 * 028 * <p>A typical implementation could look like as follows: 029 * 030 * <pre class="code"> 031 * public class MySpecialArgumentResolver implements WebArgumentResolver { 032 * 033 * public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) { 034 * if (methodParameter.getParameterType().equals(MySpecialArg.class)) { 035 * return new MySpecialArg("myValue"); 036 * } 037 * return UNRESOLVED; 038 * } 039 * }</pre> 040 * 041 * @author Juergen Hoeller 042 * @since 2.5.2 043 * @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#setCustomArgumentResolvers 044 */ 045@FunctionalInterface 046public interface WebArgumentResolver { 047 048 /** 049 * Marker to be returned when the resolver does not know how to 050 * handle the given method parameter. 051 */ 052 Object UNRESOLVED = new Object(); 053 054 055 /** 056 * Resolve an argument for the given handler method parameter within the given web request. 057 * @param methodParameter the handler method parameter to resolve 058 * @param webRequest the current web request, allowing access to the native request as well 059 * @return the argument value, or {@code UNRESOLVED} if not resolvable 060 * @throws Exception in case of resolution failure 061 */ 062 @Nullable 063 Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception; 064 065}