spring異常處理規範
1)業務代碼中不捕獲任何異常
1.1)即 dao、service層的所以異常,都全部拋出到上層。使得業務代碼中沒有 try-catch
2)全都使用統一返回結果集
2.1)使用ResponseEntity結合HttpStatus.OK
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
@PutMapping("update/{id}")
public ResponseEntity update(@PathVariable("id")String id) {
try{
xxxService.update(id);
return new ResponseEntity(HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity("更新異常",-1);
}
}
附: