Skip to content

SPR-14818 - Add exception MissingServletRequestHeaderException #1218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.web.bind;

/**
* {@link ServletRequestBindingException} subclass that indicates a missing header.
*
* @author Austin Song
* @since 4.3.3
*/
@SuppressWarnings("serial")
public class MissingServletRequestHeaderException extends ServletRequestBindingException {

private final String headerName;

private final String headerType;


/**
* Constructor for MissingServletRequestHeaderException.
* @param headerName the name of the missing header
* @param headerType the expected type of the missing header
*/
public MissingServletRequestHeaderException(String headerName, String headerType) {
super("");
this.headerName = headerName;
this.headerType = headerType;
}


@Override
public String getMessage() {
return "Required " + this.headerType+ " header '" + this.headerName+ "' " +
"is not present";
}

/**
* Return the name of the offending header.
*/
public final String getHeaderName() {
return this.headerName;
}

/**
* Return the expected type of the offending header.
*/
public final String getHeaderType() {
return this.headerType;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.MissingServletRequestHeaderException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.context.request.NativeWebRequest;
Expand Down Expand Up @@ -80,9 +80,8 @@ protected Object resolveName(String name, MethodParameter parameter, NativeWebRe
}

@Override
protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException {
throw new ServletRequestBindingException("Missing request header '" + name +
"' for method parameter of type " + parameter.getNestedParameterType().getSimpleName());
protected void handleMissingValue(String name, MethodParameter parameter) throws MissingServletRequestHeaderException{
throw new MissingServletRequestHeaderException(name, parameter.getNestedParameterType().getTypeName());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.MissingServletRequestHeaderException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
Expand Down Expand Up @@ -187,7 +188,7 @@ public void resolveDefaultValueFromRequest() throws Exception {
assertEquals("/bar", result);
}

@Test(expected = ServletRequestBindingException.class)
@Test(expected = MissingServletRequestHeaderException.class)
public void notFound() throws Exception {
resolver.resolveArgument(paramNamedValueStringArray, null, webRequest, null);
}
Expand Down