spring项目三层结构中,service层的返回两种方案, 其调用分别对应于下列controller中的方法
方式一
@GetMapping("/getInfo")
public ResponseEntity<Info> getInfo(String productId) {
Info info = infoService.getInfo(productId);
return ResponseEntity.success(info);
}
方式二
@GetMapping("/getInfo")
public ResponseEntity<Info> getInfo(String productId) {
return infoService.getInfo(productId);
}
大家一般用哪种? 第一种的话,如果infoService.getInfo(infoId)方法中有一些状况,譬如先根据productId查到product,然后再查info,结果发现productId查到product为null,需要返回
return ResponseEntity.fail(FAIL_MSG.CASH_LOAN_ORDER_UNFOUND);
如果用第一种,则需要再service方法中抛出异常,controller捕捉异常判断然后返回如上,二第二种方式可以直接在ervice方法中return ResponseEntity.fail(FAIL_MSG.CASH_LOAN_ORDER_UNFOUND),但是感觉service方法返回一个ResponseEntity怪怪的
第一种
抛异常 + 全局异常处理