我有,我想处理使用的所有未映射的网址,一个简单的Spring MVC应用程序@ControllerAdvice
。这里是控制器:
@ControllerAdvice
public class ExceptionHandlerController {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NoHandlerFoundException.class)
public String handle404() {
return "exceptions/404page";
}
}
尽管如此,每次都获得白色标签错误页面。
我尝试使用RuntimeException.class
,HttpStatus.BAD_REQUEST
并且延伸至与类NoHandlerFoundException
,但没有用。
有什么建议么?
为了使它的工作,你需要设置throwExceptionIfNoHandlerFound
上DispecherServlet财产。你可以做到这一点:
spring.mvc.throwExceptionIfNoHandlerFound=true
在application.properties
文件中,否则请求都将被转发到默认的servlet和NoHandlerFoundException将永远被抛出。
问题是,即使有这样的配置,这是行不通的。从文档:
请注意,如果使用org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,然后请求都将被转发到默认的servlet和NoHandlerFoundException绝不会在这种情况下被抛出。
因为春天开机默认使用org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
你也会有这样的使用自己的重写WebMvcConfigurer
:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
// Do nothing instead of configurer.enable();
}
}
当然,上面的类可能更复杂,你的情况。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句