我在 Java 春季启动代码中使用了以下 DTO 对象。
异常响应DTO.java
package be.bytebrain.hrms.exception;
import java.time.LocalDateTime;
import lombok.Data;
@Data
public class ExceptionResponseDTO {
private String message;
private LocalDateTime dateTime;
}
我的全局类带有切入点表达式来处理 spring-boot 控制器抛出的异常。
package be.bytebrain.hrms.exception;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NotFoundException.class)
public ResponseEntity<Object> handleExceptions(NotFoundException exception, WebRequest webRequest) {
var response = new ExceptionResponseDTO();
response.setDateTime(LocalDateTime.now());
response.setMessage(exception.getMessage());
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, String> handleValidationExceptions(MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors().forEach((error) -> {
String fieldName = ((FieldError) error).getField();
String errorMessage = error.getDefaultMessage();
errors.put(fieldName, errorMessage);
});
return errors;
}
}
尝试编译 spring boot 项目时出现错误。
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/naveenvignesh/Projects/hrms/hrms-api/src/main/java/be/bytebrain/hrms/exception/GlobalExceptionHandler.java:[24,25] cannot find symbol
symbol: method setDateTime(java.time.LocalDateTime)
location: variable response of type be.bytebrain.hrms.exception.ExceptionResponseDTO
[ERROR] /Users/naveenvignesh/Projects/hrms/hrms-api/src/main/java/be/bytebrain/hrms/exception/GlobalExceptionHandler.java:[25,25] cannot find symbol
symbol: method setMessage(java.lang.String)
location: variable response of type be.bytebrain.hrms.exception.ExceptionResponseDTO
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.712 s
[INFO] Finished at: 2021-05-25T18:05:40+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project be.bytebrain.hrms: Compilation failure: Compilation failure:
[ERROR] /Users/naveenvignesh/Projects/hrms/hrms-api/src/main/java/be/bytebrain/hrms/exception/GlobalExceptionHandler.java:[24,25] cannot find symbol
[ERROR] symbol: method setDateTime(java.time.LocalDateTime)
[ERROR] location: variable response of type be.bytebrain.hrms.exception.ExceptionResponseDTO
[ERROR] /Users/naveenvignesh/Projects/hrms/hrms-api/src/main/java/be/bytebrain/hrms/exception/GlobalExceptionHandler.java:[25,25] cannot find symbol
[ERROR] symbol: method setMessage(java.lang.String)
[ERROR] location: variable response of type be.bytebrain.hrms.exception.ExceptionResponseDTO
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException