处理异常
rest httpapi
HttpStatus (500,404等状态码)、ResponseEntity(返回实体)(T t,HttpStatus hs)
在类级别使用@ExceptionHandler
使用@ControllerAdvice,捕获所有控制器或控制器的一个子集所抛出的全局异常
package masterSpringMvc.error; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; @ControllerAdvice public class EntityNotFoundMapper { @ExceptionHandler(EntityNotFoundException.class) @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Entity could not be found") public void handleNotFound() { } }
处理控制器抛出的所有 EntityNotFoundException 异常
Spring HATEOAS 和 Spring Data REST