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 023 * that a request header expected in the method parameters of an 024 * {@code @RequestMapping} method is not present. 025 * 026 * @author Juergen Hoeller 027 * @since 5.1 028 * @see MissingRequestCookieException 029 */ 030@SuppressWarnings("serial") 031public class MissingRequestHeaderException extends ServletRequestBindingException { 032 033 private final String headerName; 034 035 private final MethodParameter parameter; 036 037 038 /** 039 * Constructor for MissingRequestHeaderException. 040 * @param headerName the name of the missing request header 041 * @param parameter the method parameter 042 */ 043 public MissingRequestHeaderException(String headerName, MethodParameter parameter) { 044 super(""); 045 this.headerName = headerName; 046 this.parameter = parameter; 047 } 048 049 050 @Override 051 public String getMessage() { 052 return "Missing request header '" + this.headerName + 053 "' for method parameter of type " + this.parameter.getNestedParameterType().getSimpleName(); 054 } 055 056 /** 057 * Return the expected name of the request header. 058 */ 059 public final String getHeaderName() { 060 return this.headerName; 061 } 062 063 /** 064 * Return the method parameter bound to the request header. 065 */ 066 public final MethodParameter getParameter() { 067 return this.parameter; 068 } 069 070}