2

springboot中controller与service间try/catch的性能问题。

 2 years ago
source link: https://www.oschina.net/question/4445894_2324268
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

springboot中controller与service间try/catch的性能问题。

玖戈 发布于 11/02 11:24
阅读 495

咨询下关于try/catch性能,及使用的问题。

背景:个人在service中处理业务时(比如字段校验)习惯使用

throw new RuntimeException("xxx内容不能为空")

这样我可以从controller中使用try/catch中使用

try {
    boolean result = service.result();
    if(result){
        return CommonResponse.success();
    } else {
        return CommonResponse.error(errorCode, errorMsg);
    }
} catch (RuntimeException e) {
    return CommonResponse.error(errorCode, e.getMessage());
}

的方式返回给前端数据(commonResponse是一个公共返回)。

请问这样对性能的影响大吗?可以这样写吗?和直接return service.result()相比那种比较好呢?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK