001/* 002 * Copyright 2002-2015 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.servlet; 018 019import javax.servlet.ServletException; 020import javax.servlet.http.HttpServletRequest; 021import javax.servlet.http.HttpServletResponse; 022 023import org.springframework.http.HttpHeaders; 024 025/** 026 * By default when the DispatcherServlet can't find a handler for a request it 027 * sends a 404 response. However if its property "throwExceptionIfNoHandlerFound" 028 * is set to {@code true} this exception is raised and may be handled with 029 * a configured HandlerExceptionResolver. 030 * 031 * @author Brian Clozel 032 * @since 4.0 033 * @see DispatcherServlet#setThrowExceptionIfNoHandlerFound(boolean) 034 * @see DispatcherServlet#noHandlerFound(HttpServletRequest, HttpServletResponse) 035 */ 036@SuppressWarnings("serial") 037public class NoHandlerFoundException extends ServletException { 038 039 private final String httpMethod; 040 041 private final String requestURL; 042 043 private final HttpHeaders headers; 044 045 046 /** 047 * Constructor for NoHandlerFoundException. 048 * @param httpMethod the HTTP method 049 * @param requestURL the HTTP request URL 050 * @param headers the HTTP request headers 051 */ 052 public NoHandlerFoundException(String httpMethod, String requestURL, HttpHeaders headers) { 053 super("No handler found for " + httpMethod + " " + requestURL); 054 this.httpMethod = httpMethod; 055 this.requestURL = requestURL; 056 this.headers = headers; 057 } 058 059 060 public String getHttpMethod() { 061 return this.httpMethod; 062 } 063 064 public String getRequestURL() { 065 return this.requestURL; 066 } 067 068 public HttpHeaders getHeaders() { 069 return this.headers; 070 } 071 072}