spring NoHandlerFoundException 源码

  • 2022-08-08
  • 浏览 (391)

spring NoHandlerFoundException 代码

文件路径:/spring-webmvc/src/main/java/org/springframework/web/servlet/NoHandlerFoundException.java

/*
 * Copyright 2002-2022 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
 *
 *      https://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.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.web.ErrorResponse;

/**
 * By default, when the DispatcherServlet can't find a handler for a request it
 * sends a 404 response. However, if its property "throwExceptionIfNoHandlerFound"
 * is set to {@code true} this exception is raised and may be handled with
 * a configured HandlerExceptionResolver.
 *
 * @author Brian Clozel
 * @since 4.0
 * @see DispatcherServlet#setThrowExceptionIfNoHandlerFound(boolean)
 * @see DispatcherServlet#noHandlerFound(HttpServletRequest, HttpServletResponse)
 */
@SuppressWarnings("serial")
public class NoHandlerFoundException extends ServletException implements ErrorResponse {

	private final String httpMethod;

	private final String requestURL;

	private final HttpHeaders headers;

	private final ProblemDetail body;


	/**
	 * Constructor for NoHandlerFoundException.
	 * @param httpMethod the HTTP method
	 * @param requestURL the HTTP request URL
	 * @param headers the HTTP request headers
	 */
	public NoHandlerFoundException(String httpMethod, String requestURL, HttpHeaders headers) {
		super("No endpoint " + httpMethod + " " + requestURL + ".");
		this.httpMethod = httpMethod;
		this.requestURL = requestURL;
		this.headers = headers;
		this.body = ProblemDetail.forStatusAndDetail(getStatusCode(), getMessage());
	}

	@Override
	public HttpStatusCode getStatusCode() {
		return HttpStatus.NOT_FOUND;
	}

	public String getHttpMethod() {
		return this.httpMethod;
	}

	public String getRequestURL() {
		return this.requestURL;
	}

	public HttpHeaders getHeaders() {
		return this.headers;
	}

	@Override
	public ProblemDetail getBody() {
		return this.body;
	}

}

相关信息

spring 源码目录

相关文章

spring AsyncHandlerInterceptor 源码

spring DispatcherServlet 源码

spring FlashMap 源码

spring FlashMapManager 源码

spring FrameworkServlet 源码

spring HandlerAdapter 源码

spring HandlerExceptionResolver 源码

spring HandlerExecutionChain 源码

spring HandlerInterceptor 源码

spring HandlerMapping 源码

0  赞