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 * @see MissingMatrixVariableException
031 */
032@SuppressWarnings("serial")
033public class MissingPathVariableException extends ServletRequestBindingException {
034
035        private final String variableName;
036
037        private final MethodParameter parameter;
038
039
040        /**
041         * Constructor for MissingPathVariableException.
042         * @param variableName the name of the missing path variable
043         * @param parameter the method parameter
044         */
045        public MissingPathVariableException(String variableName, MethodParameter parameter) {
046                super("");
047                this.variableName = variableName;
048                this.parameter = parameter;
049        }
050
051
052        @Override
053        public String getMessage() {
054                return "Missing URI template variable '" + this.variableName +
055                                "' for method parameter of type " + this.parameter.getNestedParameterType().getSimpleName();
056        }
057
058        /**
059         * Return the expected name of the path variable.
060         */
061        public final String getVariableName() {
062                return this.variableName;
063        }
064
065        /**
066         * Return the method parameter bound to the path variable.
067         */
068        public final MethodParameter getParameter() {
069                return this.parameter;
070        }
071
072}