gpt4 book ai didi

java - Spring @ControllerAdvice Handler 异常没有选择 NoHandlerFoundException

转载 作者:行者123 更新时间:2023-11-30 06:33:49 25 4
gpt4 key购买 nike

我正在使用@ControllerAdvice处理异常,我的其余 Controller 有@ControllerAdvice,非休息 Controller 有@ControllerAdvice,他们正在为Exception.class选择异常,但他们没有选择NoHandlerFoundException.class。

我们使用 Spring 4.3.7.RELEASE、Java Config、Apache 8.5.12。

下面是 MyWebInitializer 类

public class MyWebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {


@Override
public void onStartup(ServletContext servletContext) throws ServletException {

super.onStartup(servletContext);

servletContext.addListener(new RequestContextListener());
servletContext.addListener(new SpringSessionListener());

ServletRegistration.Dynamic dispatcher1 =
servletContext.addServlet("blackwellsjspservlet", new JspServlet());
dispatcher1.setLoadOnStartup(3);
dispatcher1.addMapping("*.do");
}

@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { SpringRootConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { SpringWebConfig.class };
}

@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}

@Override
protected String getServletName() {
return "blackwellsmvcdispatcherservlet";
}

//Added to allow customised NoHandlerFoundPage
@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
boolean done = registration.setInitParameter("throwExceptionIfNoHandlerFound", "true"); // -> true
if(!done) throw new RuntimeException();
}

//Added to register a default profile
@Override
protected WebApplicationContext createServletApplicationContext() {
WebApplicationContext context = (WebApplicationContext) super.createServletApplicationContext();
((ConfigurableEnvironment) context.getEnvironment()).setDefaultProfiles("production");

return context;
}
}

我的 SpringWebConfig 是:

@EnableWebMvc
@Configuration
public class SpringWebConfig extends WebMvcConfigurerAdapter {

@Autowired
private ServletContext servletContext;

@Autowired
Environment env;

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/");
viewResolver.setViewNames("*.jsp");
return viewResolver;
}


@Bean
public Loader<?> templateLoader(){
return new ServletLoader(servletContext);
}

@Bean
public CustomSpringExtension springExtension() {
return new CustomSpringExtension();
}



@Bean
public PebbleEngine pebbleEngine() {
return new PebbleEngine.Builder()
.loader(this.templateLoader()).cacheActive(false)
.extension(springExtension())
.build();
}

@Bean
public ViewResolver pebbleViewResolver() {
PebbleViewResolver viewResolver = new PebbleViewResolver();
viewResolver.setPrefix("/WEB-INF/patternLab/");
viewResolver.setPebbleEngine(pebbleEngine());
viewResolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5);
viewResolver.setViewNames("*.html");
return viewResolver;
}

/**
* Bean for ResourceMessageBundle
* basename and folder resources/locale/messages. i.g. messages_en_GB.properties,messages_en_US.properties
* @author Henrique Droog
* @return
*/
@Bean
public MessageSource messageSource(){
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:locale/messages");
messageSource.setDefaultEncoding("UTF-8");

return messageSource;
}

/**
* Bean for LocalResolver
* set en as a default locale
* @author Henrique Droog
* @date 09.03.2017
* @return
*/
@Bean
public LocaleResolver localeResolver(){
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setDefaultLocale(new Locale("en"));
resolver.setCookieName("myLocaleCookie");
resolver.setCookieMaxAge(4800);
return resolver;
}

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor(){
return new MethodValidationPostProcessor();
}


@Profile({"production-api","dev-api"})
@ComponentScan(basePackages = { "uk.co.blackwells.controllers", "uk.co.blackwells.rest.controller"})
public static class ProductionAPIWebConfig {

}

@Profile({"production","dev","default"})
@ComponentScan(basePackages = { "uk.co.blackwells.controllers","uk.co.blackwells.rest.controller"})
public static class ProductionWebConfig {

}

@Profile({"test"})
@ComponentScan(basePackages = { "uk.co.blackwells.controllers","uk.co.blackwells.rest.controller"})
public static class TestWebConfig {

}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(sessionInterceptor()).addPathPatterns("/**").excludePathPatterns("/api/**", "/restful/**");

LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("mylocale");
registry.addInterceptor(interceptor);

}

@Bean
public SessionInterceptor sessionInterceptor() {
return new SessionInterceptor();
}

@Bean
public Validator validatorFactory () {
return new LocalValidatorFactoryBean();
}
}

我对非休息 Controller 的 Controller 建议:

@ControllerAdvice(annotations=Controller.class)
public class GlobalDefaultExceptionHandlerController {

private final Logger logger = LoggerFactory.getLogger(GlobalDefaultExceptionHandlerController.class);
ErrorList errorList;

/**
* This init binder register a custom editor to trim all fields coming
* from html
* @param binder
*/
@InitBinder
public void initBinder ( WebDataBinder binder )
{
StringTrimmerEditor stringtrimmer = new StringTrimmerEditor(true);
binder.registerCustomEditor(String.class, stringtrimmer);
}

@ExceptionHandler(NoHandlerFoundException.class)
public ModelAndView handleNotFoundException(HttpServletRequest req, NoHandlerFoundException ex) {

ErrorMessage erMsg = new ErrorMessage();
erMsg.setError(ex.getMessage());
erMsg.setStackTrace(stackTrace);
erMsg.setException(ex);

ModelAndView model = new ModelAndView();
model.addObject("errorMessageObject", erMsg);

model.setViewName("pages/error-404.html");

return model;
}

@ExceptionHandler(Exception.class)
public ModelAndView handleAllException(HttpServletRequest req, Exception ex) throws Exception {

// Rethrow annotated exceptions or they will be processed here instead.
if (AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class) != null)
throw ex;


ErrorMessage erMsg = new ErrorMessage();
erMsg.setError(exceptionCode);
erMsg.setStackTrace(stackTrace);
erMsg.setException(ex);

ModelAndView model = new ModelAndView();
model.addObject("errorMessageObject", erMsg);
model.addObject("errMsg", ex.getMessage());
model.addObject("exceptionCode", exceptionCode);

model.setViewName("pages/error-500.html");

return model;

}

@RequestMapping(value = {"/SessionError"}, method = RequestMethod.GET)
public String sessionError() {
return "pages/error-500.html";
}
}

对于休息 Controller :

@RestControllerAdvice(annotations=RestController.class)
public class ApiExceptionHandlerController {

private final Logger logger = LoggerFactory.getLogger(ApiExceptionHandlerController.class);

@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleException(HttpServletRequest req, Exception ex){
logger.error("API Exception: {}", ex.getMessage(), ex);
logger.error("Path:{}, IP:{}", req.getServletPath(), req.getRemoteAddr());

ApiError apiError =
new ApiError(HttpStatus.INTERNAL_SERVER_ERROR, "Internal server error", ex.getMessage());
return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
}

@ExceptionHandler(NoHandlerFoundException.class)
public ResponseEntity<Object> handleNotFoundException(HttpServletRequest req, Exception ex){
logger.error("API NoHandlerFoundException: {}", ex.getMessage(), ex);
logger.error("Path:{}, IP:{}", req.getServletPath(), req.getRemoteAddr());
ex.printStackTrace();

ApiError apiError =
new ApiError(HttpStatus.NOT_FOUND, ex.getMessage(), ex.getMessage());
return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
}
}

直到某个时候,两个 Controller 建议都选择了 NoHandlerFoundException。我查看了这些文件的历史更改,但它们已经有一段时间没有更改了。我想也许这部分代码

@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
boolean done = registration.setInitParameter("throwExceptionIfNoHandlerFound", "true"); // -> true
if(!done) throw new RuntimeException();
}

正在被其他一些代码覆盖,但我找不到与此相关的任何内容。

最佳答案

如果您使用 Spring Boot:

在 org.springframework.web.servlet.DispatcherServlet 类中有一个名为 throwExceptionIfNoHandlerFound 的变量。如果将此设置为 true,则名为 noHandlerFound 的方法将抛出 NoHandlerFoundException。您的异常处理程序现在将捕获它。

将此属性添加到您的 application.properties 文件中: spring.mvc.throw-exception-if-no-handler-found=true

关于java - Spring @ControllerAdvice Handler 异常没有选择 NoHandlerFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45566485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com