gpt4 book ai didi

Exception caught by @ExceptionHandler is not returned to client as a response, rather jetty is returning generic status code 500, why?(@ExceptionHandler捕获的异常不会作为响应返回给客户端,而是Jetty返回通用状态代码500,为什么?)

翻译 作者:bug小助手 更新时间:2023-10-26 22:40:48 31 4
gpt4 key购买 nike



This is the global exception handler class that should deal with the exceptions:

这是应该处理异常的全局异常处理程序类:


package org.example.myapp.exceptions;

@RestControllerAdvice
public class ExceptionHandlerAdvice {

private record ErrorContent(Instant timestamp, String errorMessage){}

@ExceptionHandler(SkillNotFoundException.class)
public ResponseEntity<ErrorContent> handleSkillNotFoundException(SkillNotFoundException exception){
System.out.println("handle me pls!"); //<------- this is being printed, so I assume this exception handler method caught the exception
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorContent(
Instant.now(),
exception.getMessage()
));
}

}

Config class for the DispacherServlet:

DispacherServlet的配置类:


package org.example.myapp.config;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"org.example.myapp.controllers", "org.example.myapp.exceptions"})
@EnableSpringDataWebSupport
public class ServletConfig implements WebMvcConfigurer {}

Config class for the root app:

根应用程序的配置类:


package org.example.myapp.config;

@Configuration
@ComponentScan(basePackages = "org.example.myapp")
@EnableJpaRepositories(basePackages = "org.example.myapp.repositories")
@EnableTransactionManagement
public class RootConfig {

/////SOME JPA/HIBERNATE CONFIGURATIONS HERE

}

Class to delegate configs:

要委派配置的类:


package org.example.myapp.config;

public class AppConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] {RootConfig.class};
}

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

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

So the flow is:
Controller gets request -> service method gets called -> repo method gets called -> service method throws SkillNotFoundException -> exception delegation ends up on this Advice class -> appropriate exception handler method is found and called -> as i said above the System.out message is printed but return is ignored(?) -> exception message is appropriately logged in stack trace as well -> jetty returns status code 500 to my postman instead of what I wanted from the exception handler message.

因此流程是:控制器获取请求->调用服务方法->调用repo方法->服务方法抛出SgarNotFoundException->异常委托在这个通知类上结束->找到并调用了适当的异常处理程序方法->正如我上面所说的,打印了System.out消息,但忽略了返回(?)->异常消息也被适当地记录在堆栈跟踪中->Jetty向我的邮递员返回状态代码500,而不是我想从异常处理程序消息中得到的。


Any solutions to this?

对此有什么解决方案吗?


更多回答

I tried reproducing your Advice class and running locally with Spring Boot it returned 404 as desired when checking in the browser dev tools.

我尝试重新生成您的建议类,并使用Spring Boot在本地运行,在签入浏览器开发工具时,它按预期返回了404。

Well, I'm not working in spring boot, I'm working in standard spring, which could mean I am missing some additional configuration for something? @scrhartley

嗯,我不是在用弹簧靴子工作,我是在用标准弹簧靴子工作,这可能意味着我缺少了一些额外的配置?@scrhartley

优秀答案推荐

Problem solved: rebooted my PC.....
I have no idea why this fixed it but it did.....

问题已解决:已重新启动我的PC.....我不知道为什么这个解决了它,但它确实..。


更多回答

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