gpt4 book ai didi

java - ExceptionHandler,得到 java.lang.IllegalStateException : Could not resolve method parameter at index 0?

转载 作者:行者123 更新时间:2023-12-02 01:47:54 28 4
gpt4 key购买 nike

我对 Java Spring 的 ExceptionHandler 有问题。我有一个名为 EntityNotFoundException 的异常,我想在引发异常时从 REST Controller 调用 ExceptionHandler 方法。

这是我的 REST Controller 方法代码:

@ExceptionHandler(Exception.class)
public ResponseEntity insertTicket(@Valid @RequestBody Ticket ticket, @AuthenticationPrincipal Principal principal) throws EntityNotFoundException {
ticket.setCreationTimestamp(Instant.now());
ticket.setSource(TicketSource.CLIENT);
ticket.setCurrentTicketStatus(TicketStatus.VALIDATION);
User customer = userController.findUserByUsername(principal.getName());
ticket.setCustomer(customer);
try {
ticket.setAttachments(savedFiles(
ticket.getAttachments(),
ticket.getCustomer().getUsername()
));
} catch (FileUploadException e) {
return CommonResponseEntity.NotFoundResponseEntity("ENTITY_NOT_FOUND");
}
ticketController.insertTicket(ticket);

mailSenderController.sendMail(customer.getEmail(), "TICKET_OPENED");

return CommonResponseEntity.CreatedResponseEntity("CREATED");
}

这是我的异常处理程序代码:

@EnableWebMvc
@ControllerAdvice
@RestControllerAdvice
public class InterceptedResponseEntityExceptionHandler extends
ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
BindingResult bindingResult = ex.getBindingResult();

List<MethodArgumentFieldError> methodArgumentFieldErrors = bindingResult
.getFieldErrors()
.stream()
.map(fieldError -> new MethodArgumentFieldError(fieldError.getField(), fieldError.getCode(), fieldError.getRejectedValue()))
.collect(Collectors.toList());

List<MethodArgumentGlobalError> methodArgumentGlobalErrors = bindingResult
.getGlobalErrors()
.stream()
.map(globalError -> new MethodArgumentGlobalError(globalError.getCode()))
.collect(Collectors.toList());

MethodArgumentError methodArgumentError = new MethodArgumentError(methodArgumentFieldErrors, methodArgumentGlobalErrors);

return new ResponseEntity<>(methodArgumentError, HttpStatus.UNPROCESSABLE_ENTITY);
}

@Override
protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
MissingParameterError missingParameterError = new MissingParameterError(ex.getParameterName(), ex.getMessage());

return new ResponseEntity<>(missingParameterError, HttpStatus.UNPROCESSABLE_ENTITY);
}



@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleNotFound(Exception ex, WebRequest request) {

System.out.println("inside!");

if( ex instanceof DataIntegrityViolationException){
System.out.println("Data integrity violation");
String constraintViolationErrors = ex.getMessage();
String msgErr = (constraintViolationErrors.substring(constraintViolationErrors.indexOf("=") + 1));
return new ResponseEntity<>(msgErr, HttpStatus.BAD_REQUEST);
}

if(ex instanceof UsernameNotFoundException) {
String msgErr = ex.getMessage();
return new ResponseEntity<>(msgErr, HttpStatus.BAD_REQUEST);
}


if (ex instanceof NotFoundEntityException || ex instanceof EntityNotFoundException || ex instanceof NoSuchElementException){
//return CommonResponseEntity.NotFoundResponseEntity(ex.getMessage());
System.out.println("inside the handler!");
return new ResponseEntity<>(ex.getMessage(),HttpStatus.NOT_FOUND);
}

if(ex instanceof UpdateException){
return new ResponseEntity<>(HttpStatus.CONFLICT);
}

return null;
}





@Data
@AllArgsConstructor
public class MethodArgumentError {
private List<MethodArgumentFieldError> fieldErrors;
private List<MethodArgumentGlobalError> globalErrors;
}

@Data
@AllArgsConstructor
public class MethodArgumentFieldError {
private String field;
private String code;
private Object rejectedValue;
}

@Data
@AllArgsConstructor
public class MethodArgumentGlobalError {
private String code;
}

@Data
@AllArgsConstructor
public class MissingParameterError {
private String parameterName;
private String message;
}

@Data
@AllArgsConstructor
public class ConstraintViolationError {
private String invalidValue;
private String message;
}

}

我不知道为什么,当我收到 DataIntegrityViolationException 时,会调用 ExceptionHandler,而当我收到 EntitynotFoundException 时,我会收到以下消息:

java.lang.IllegalStateException: Could not resolve method parameter at index 0 in public org.springframework.http.ResponseEntity com.isssr.ticketing_system.rest.TicketRest.insertTicket(com.isssr.ticketing_system.entity.Ticket,java.security.Principal) throws com.isssr.ticketing_system.exception.EntityNotFoundException: No suitable resolver for argument 0 of type 'com.isssr.ticketing_system.entity.Ticket'

有什么问题吗?

最佳答案

我还看到了其他奇怪的事情;我收到此消息:

无法调用@ExceptionHandler方法:public org.springframework.http.ResponseEntity com.isssr.ticketing_system.rest.TicketRest.insertTicket(com.isssr.ticketing_system.entity.Ticket,java.security.Principal) throws com.isssr .ticketing_system.exception.EntityNotFoundException

看来 Spring 正在尝试调用另一个方法而不是我的 ExceptionHandler 的方法。这怎么可能?

关于java - ExceptionHandler,得到 java.lang.IllegalStateException : Could not resolve method parameter at index 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57445196/

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