Skip to content
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

Spring not throwing NoHandlerFoundException anymore #9263

Closed
Harown opened this Issue May 18, 2017 · 11 comments

Comments

Projects
None yet
8 participants
@Harown
Copy link

Harown commented May 18, 2017

After we upgraded from Spring Boot 1.4.0 to 1.5.3 our web application does not throw the NoHandlerFoundException anymore. Configuration looks like the following:

in application.properties:

spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false

the corresponding exception controller:

@ControllerAdvice
@EnableWebMvc
public class ExceptionController 
{
       // works
       @ExceptionHandler(AccessDeniedException.class)
	public String handleAccessDeniedException(AccessDeniedException ex, HttpServletRequest request) {
		return "403";
	}

	// doesn't work anymore
	@ExceptionHandler(NoHandlerFoundException.class)
	public String handleNotFoundError(NoHandlerFoundException ex, HttpServletRequest request) {
		return "404";
	}
}

We had no problems with Spring 1.4.0 but it stopped working in 1.5.3. Why is the exception not thrown anymore?

@wilkinsona

This comment has been minimized.

Copy link
Member

wilkinsona commented May 18, 2017

Did you have @EnableWebMvc with both 1.4 and 1.5? I would expect it to turn off the auto-configuration of Spring MVC making spring.mvc.throw-exception-if-no-handler-found=true have no effect

@Harown

This comment has been minimized.

Copy link
Author

Harown commented May 18, 2017

Unfortunately, omitting @EnableWebMvc has no effect.

@wilkinsona

This comment has been minimized.

Copy link
Member

wilkinsona commented May 18, 2017

Something that is, I think, the same as you are doing works for me with both 1.4.6 and 1.5.3. Can you please provide a small sample that reproduces the problem you're seeing? Something that works with 1.4.0 but fails with 1.5.3 would be ideal.

@Harown

This comment has been minimized.

Copy link
Author

Harown commented May 18, 2017

While preparing a sample application we've found the culprit:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
	private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
	        "classpath:/META-INF/resources/", "classpath:/resources/",
	        "classpath:/static/", "classpath:/public/" };
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
	    registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
	}
}

Replacing the URl pattern with the subfolder containing the static resources spring.mvc.throw-exception-if-no-handler-found=true have the desired effect. Thank you.

@Berg-it

This comment has been minimized.

Copy link

Berg-it commented Feb 19, 2018

By default, the DispatcherServlet does not throw a NoHandlerFoundException. You need to enable that:

I use Spring Boot 1.5.4.RELEASE:

I added
@EnableWebMvc

and in my prop file I added

server.error.whitelabel.enabled: false
spring.mvc.throw-exception-if-no-handler-found: true

@bclozel

This comment has been minimized.

Copy link
Member

bclozel commented Feb 19, 2018

@Berg-it adding @EnableWebMvc should totally switch off Spring Boot's auto-configuration for Spring MVC - so all spring.mvc.* configuration properties will be ignored. See the reference documentation on that.

@Berg-it

This comment has been minimized.

Copy link

Berg-it commented Feb 19, 2018

@bclozel: You are right! I'll see how can I active this property. Thnak you

@AndRossi

This comment has been minimized.

Copy link

AndRossi commented Dec 27, 2018

Sorry to re-open this issue, but I am experiencing a very closely related problem.
I am developing a small webapp with Spring Boot, HTML controllers and Thymeleaf.

I am not using @EnableWebMvc (and I would prefer not to use it).
What I would like to do is both these two things :

  • make sure that an exception is thrown if no handlers are found
  • have my static resources available for the client

Is there a way to accomplish this?

My issue is basically the same as this StackOverflow page: https://stackoverflow.com/questions/39973945/no-handler-found-exception-and-static-recources-in-spring-boot

Thanks.

@bclozel

This comment has been minimized.

Copy link
Member

bclozel commented Dec 27, 2018

@AndRossi the answer on that SO thread is right.

When the resource handler doesn't find a static resource to serve, it's not delegating to the next handler in line, but rather responding with an HTTP 404. The only way to achieve what you want is to map your static resources under a specific sub-path like /static configure the spring.mvc.throw-exception-if-no-handler-found configuration property. That exception will be sent as long as the static resources handler is not involved.

@AndRossi

This comment has been minimized.

Copy link

AndRossi commented Dec 27, 2018

Hi @bclozel, thank you for the lightning-fast answer!

Unfortunately I have tried that answer but it does not seem to work in my scenario.
This is what my configuration currently looks like:

spring.thymeleaf.cache:false

spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB

spring.datasource.url=jdbc:postgresql://localhost:5432/db-name
spring.datasource.username=...
spring.datasource.password=...
spring.jpa.properties.hibernate.default_schema=db.name

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.hibernate.ddl-auto = update

spring.mvc.throw-exception-if-no-handler-found=true
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/static/

In my environment, it does allow the client to get the static resources, but missing URIs do not throw any exceptions :/

@bclozel

This comment has been minimized.

Copy link
Member

bclozel commented Dec 27, 2018

You're mapping everything with your static path pattern, so this behavior is expected. For further questions, please use StackOverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.