[JAVA] Throw an exception and catch when there is no handler corresponding to the path in spring

If there is no path corresponding to Servlet mapping in spring-mvc, the following warning will appear in the log. In this case, throw an exception on the spring side and catch it. This will handle the 404 (using spring instead of web.xml).

[org.springframework.web.servlet.PageNotFound](default task-2) No mapping found for HTTP request with URI [/xxxx] in DispatcherServlet with name 'dispatcher'



import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebMVCApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	protected Class<?>[] getRootConfigClasses() {
		Class<?>[] configurations = {AppConfiguration.class};
		return configurations;

	protected Class<?>[] getServletConfigClasses() {
		Class<?>[] configs = {};
		return configs;

	protected String[] getServletMappings() {
		String[] mappings = {"/"};
		return mappings;

	protected DispatcherServlet createDispatcherServlet(WebApplicationContext servletAppContext) {
		DispatcherServlet dispatcherServlet = (DispatcherServlet)super.createDispatcherServlet(servletAppContext);
		return dispatcherServlet;

By setting setThrowExceptionIfNoHandlerFound to true, an exception will be thrown if the handler corresponding to the request is not found.

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

public class NoHandlerFoundControllerAdvice {
    public ResponseEntity<String> handleNoHandlerFoundException(NoHandlerFoundException ex) {
    	return new ResponseEntity<>("not found", HttpStatus.NOT_FOUND);

Create a class that assigns ControllerAdvice (or ControllerAdvice) to describe exception handling common to multiple controllers. Since I set spring to throw NoHandlerFoundException if there is no handler corresponding to the path, specify this in ExceptionHandler.

Source of information

Recommended Posts

Throw an exception and catch when there is no handler corresponding to the path in spring
How to output the value when there is an array in the array
When there is no output to stdout in docker log
Initial value when there is no form object property in Spring request
When reassigning to an argument in a Ruby method and then calling `super` → The reassigned one is used
N things to keep in mind when reading "Introduction to Spring" and "Introduction to Spring" in the Reiwa era
How to solve the problem when the value is not sent when the form is disabled in rails and sent
[Ruby] Thinking when there is no receiver in front of the method (it looks like)
[Rails] What to do when the error No database selected and Unknown database appears in db: migrate
Monkey patch to return the current time when an empty string is specified in Ruby's Time.parse
Settings to be made when HTML or JSP is formatted in Eclipse and the layout is disappointing
I want to make the frame of the text box red when there is an input error
ProxyFactory is convenient when you want to test AOP in Spring!
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
How to write the view when Vue is introduced in Rails?
Creating an ArrayList that allows you to throw in and retrieve the coordinates of a two-dimensional plane
Resolved the error that occurred when trying to use Spark in an environment where Java 8 and Java 11 coexist.
How to solve the problem that it is not processed normally when nesting beans in Spring Batch
Why does it stop when I'm debugging in Eclipse and there are no breakpoints? Links related to
Is there no type in Ruby?
[Swift] Rather, it is meaningful to avoid var and declare it with let only when there is a branch in the assignment of the initial value.
[JavaScript] How to display an alert when there is an input form omission
catch (Exception e) or catch (IOException e) is not required in the try-with-resources syntax
What to do and how to install when an error occurs in DXRuby 1.4.7
Is there an engineer who hasn't validated the Json column in Rails?
I want to display an error message when registering in the database
[Ruby] How to prevent errors when nil is included in the operation
Is it possible to put the library (aar) in the Android library (aar) and use it?
Spring Autowired is written in the constructor
Possibility when deploying to EC2 but nothing is displayed in the error log
If the parameter is an array, how to include it in Stopara's params.permit
When you want to notify an error somewhere when using graphql-spring-boot in Spring Boot
Summary when trying to use Solr in Java and getting an error (Solr 6.x)
How to set when "The constructor Empty () is not visible" occurs in junit
[Comma (,) is strictly prohibited in the address! ] Things to keep in mind when applying for an exam at Pearson VUE
When changing the Controller of Spring Web MVC to kotlin, @Autowired The specified component is not injected and becomes null.