001/* 002 * Copyright 2002-2018 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; 018 019import org.springframework.core.MethodParameter; 020 021/** 022 * {@link ServletRequestBindingException} subclass that indicates that a path 023 * variable expected in the method parameters of an {@code @RequestMapping} 024 * method is not present among the URI variables extracted from the URL. 025 * Typically that means the URI template does not match the path variable name 026 * declared on the method parameter. 027 * 028 * @author Rossen Stoyanchev 029 * @since 4.2 030 */ 031@SuppressWarnings("serial") 032public class MissingPathVariableException extends ServletRequestBindingException { 033 034 private final String variableName; 035 036 private final MethodParameter parameter; 037 038 039 /** 040 * Constructor for MissingPathVariableException. 041 * @param variableName the name of the missing path variable 042 * @param parameter the method parameter 043 */ 044 public MissingPathVariableException(String variableName, MethodParameter parameter) { 045 super(""); 046 this.variableName = variableName; 047 this.parameter = parameter; 048 } 049 050 051 @Override 052 public String getMessage() { 053 return "Missing URI template variable '" + this.variableName + 054 "' for method parameter of type " + this.parameter.getNestedParameterType().getSimpleName(); 055 } 056 057 /** 058 * Return the expected name of the path variable. 059 */ 060 public final String getVariableName() { 061 return this.variableName; 062 } 063 064 /** 065 * Return the method parameter bound to the path variable. 066 */ 067 public final MethodParameter getParameter() { 068 return this.parameter; 069 } 070 071}