Spring Bood @ControllerAdvice 发表于 2018-12-13 | 分类于 Java , Spring Boot , Controller UsageCase: Hello123456789101112131415161718192021222324@ControllerAdviceclass SystemErrorController { private val logger = LogManager.getLogger(SystemErrorController::class) @ExceptionHandler(Exception::class) fun handleError(ex: Exception): ResponseEntity<Any> { logger.error("handleError(): $ex") val error = Error(500, HttpStatus.INTERNAL_SERVER_ERROR.name) return ResponseEntity(error, HttpStatus.INTERNAL_SERVER_ERROR) } @ExceptionHandler(MethodArgumentNotValidException::class, MissingServletRequestParameterException::class) fun handleArgumentError(ex: Exception): ResponseEntity<Any> { logger.error("handleArgumentError(): $ex") val error = Error(ErrorCode.BODY_ARGUMENT_IS_INVALID.value, ex.message ?: "") return ResponseEntity(error, HttpStatus.BAD_REQUEST) }}// Unit TestingStandaloneMockMvcBuilder mockMvc = MockMvcBuilders.standaloneSetup(new OrderController()).setControllerAdvice(adviceController);RestAssuredMockMvc.standaloneSetup(mockMvc);