我试图在Intellij Idea中运行应用程序,但我收到此错误....
org.springframework.beans.factory.BeanCreationException:创建名为“globalExceptionHandler”的bean时出错:查找方法解析失败;嵌套异常为java. lang. IllegalStateException:无法内省类[com]。app.exception_handler。GlobalExceptionHandler] from ClassLoader [org.springframework.boot.devtools.restart.classloader.RestartClassLoader@2ad58620] at org. SpringFramework豆子工厂。注解AutowiredAnnotationBeanPostProcessor。determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:289)~[spring-beans-5.3.9.jar:5.3.9]在org. SpringFramework豆子工厂。支持。AbstractAutowireCapableBeanFactory. determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1284)~[spring-beans-5.3.9.jar:5.3.9]在org. SpringFramework豆子工厂。支持。AbstractAutowireCapableBeanFactory. createBeanInstance(AbstractAutowireCapableBeanFactory.java:1201)~[spring-beans-5.3.9.jar:5.3.9]在org. SpringFramework豆子工厂。支持。AbstractAutowireCapableBeanFactory. doCreateBean(AbstractAutowireCapableBeanFactory.java:564)~[spring-beans-5.3.9.jar:5.3.9]在org. SpringFramework豆子工厂。支持。AbstractAutowireCapableBeanFactory. createBean(AbstractAutowireCapableBeanFactory.java:524)~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)~[spring-beans-5.3.9.jar:5.3.9]在org. SpringFramework豆子工厂。支持。DefaultSingletonBeanRegistry。getSingleton(DefaultSingletonBeanRegistry.java:234)~[spring-beans-5.3.9.jar:5.3.9]在org. SpringFramework豆子工厂。支持。AbstractBeanFactory. doGetBean(AbstractBeanFactory.java:333)~[spring-beans-5.3.9.jar:5.3.9]在org. SpringFramework豆子工厂。支持。AbstractBeanFactory. getBean(AbstractBeanFactory.java:208)~[spring-beans-5.3.9.jar:5.3.9]在org. SpringFramework豆子工厂。支持。DefaultListableBeanFactory。preInstantiateSingletons(DefaultListableBeanFactory.java:944)~[spring-beans-5.3.9.jar:5.3.9]在org. SpringFramework上下文支持。AbstractApplicationContext。finishBeanFactoryInitialization(AbstractApplicationContext.java:918)~[spring-context-5.3.9.jar:5.3.9]在org. SpringFramework上下文支持。AbstractApplicationContext。refresh(AbstractApplicationContext.java:583)~[spring-context-5.3.9.jar:5.3.9]在org. SpringFramework Boot SpringApplication。refresh(SpringApplication.java:754)~[spring-boot-2.5.4.jar:2.5.4]在org. SpringFramework Boot SpringApplication。refreshContext(SpringApplication.java:434)~[spring-boot-2.5.4.jar:2.5.4]在 www.example.com (SpringApplication.java:338) ~[spring-boot-2.5.4.jar:2.5.4] at org.springframework.boot.SpringApplication.run (SpringApplication.java:1343) ~[spring-boot-2.5.4.jar:2.5.4] at org.springframework.boot.SpringApplication.run (SpringApplication.java:1332) ~[spring-boot-2.5.4.jar:2.5.4] at com.app.MmsApplication.main(MmsApplication.java:13) ~[classes/:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na] at www.example.com (RestartLauncher.java:49) ~[spring-boot-devtools-2.5.4.jar:2.5.4]
下面是全局异常处理程序类
package com.app.exception_handler;
import java.time.LocalDateTime;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import com.app.custom_exceptions.UserHandlingException;
import com.app.dto.ErrorResponse;
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler{
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<?> handleRuntimeException(RuntimeException e) {
System.out.println("in handle run time exc " + e);
ErrorResponse resp = new ErrorResponse(e.getMessage(), LocalDateTime.now());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resp);
}
@ExceptionHandler(EmptyResultDataAccessException.class)
public ResponseEntity<?> handleEmptyDataExcetpion(EmptyResultDataAccessException e) {
System.out.println("in handle exc " + e);
ErrorResponse resp = new ErrorResponse(e.getMessage(), LocalDateTime.now());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(resp);
}
@ExceptionHandler(UserHandlingException.class)
public ResponseEntity<?> handleUserHandlingException(UserHandlingException e) {
System.out.println("in handle user exc " + e);
ErrorResponse resp = new ErrorResponse(e.getMessage(), LocalDateTime.now());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(resp);
}
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {
System.out.println("in handle invalid meth args ");
StringBuilder sb = new StringBuilder("Validation Errors : ");
ex.getBindingResult().getFieldErrors().forEach(e -> sb.append(e.getDefaultMessage()+" "));
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(sb.toString(), LocalDateTime.now()));
}
}
1条答案
按热度按时间nlejzf6q1#
我已经测试了这个代码片段--应用程序可以正常启动。
IllegalStateException
表示在非法或不适当的时间调用了某个方法。换句话说,Java环境或Java应用程序不处于所请求的操作的适当状态。