当前位置: 首页 > news >正文

顺企网上海网站建设网络推广有多少种方法

顺企网上海网站建设,网络推广有多少种方法,银川商城网站建设,怎么从建设部网站下载规范文章目录 前言一、统一的返回格式二、全局异常处理三、全局返回处理(装逼用的)总结 前言 项目中一般都会有规定好的接口返回格式,无论成功与失败,一般格式都是不变的,这样是为了方便前后端统一处理,今天就来说下前后端统一处理的较为优雅的方式; 一、统一的返回格式 一般而言…

文章目录

  • 前言
  • 一、统一的返回格式
  • 二、全局异常处理
  • 三、全局返回处理(装逼用的)
  • 总结


前言

项目中一般都会有规定好的接口返回格式,无论成功与失败,一般格式都是不变的,这样是为了方便前后端统一处理,今天就来说下前后端统一处理的较为优雅的方式;


一、统一的返回格式

一般而言都会有一个统一的返回类作为接口的返回数据的封装,例如:

@Data
public class Result<T> implements Serializable {private int code = HttpStatus.OK.value();private String msg;private T data;public static <T> Result<T> success() {return success(null);}public static <T> Result<T> failure() {return failure(HttpStatus.BAD_REQUEST.value(),HttpStatus.BAD_REQUEST.getReasonPhrase());}public static <T> Result<T> success(T data) {return new Result<T>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), data);}public static <T> Result<T> failure(int errorCode, String errorMsg) {return failure(errorCode, errorMsg, null);}public static <T> Result<T> failure(int code, String errorMsg, T data) {return new Result<T>(code, errorMsg, data);}public Result(int code, String msg, T data) {this.code = code;this.msg = msg;this.data = data;}}

然后我们通过此类作为返回参数的统一封装,这样无论成功与否,都是三个参数
code msg data

二、全局异常处理

  • 项目中难免会有异常抛出
    1. 服务端报错的异常
    2. 处理逻辑中的异常(参数校验, 逻辑不通等)
  • 由于我们一般都是前后端分离项目,所以都是接口方式的返回,那么我们只需要处理接口就可以了
@RestControllerAdvice
@Slf4j
public class ControllerExceptionHandel {@ExceptionHandler(ValidationException.class)public Result<String> handleException(ValidationException e) {e.printStackTrace();log.error("参数校验发生异常:{}", e.getMessage());return Result.failure(HttpStatus.BAD_REQUEST.value(), e.getMessage());}@ExceptionHandler(value = NullPointerException.class)public Result<String> exceptionHandler(NullPointerException e) {e.printStackTrace();log.error("空指针异常:{}", e.getMessage());return Result.failure(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());}@ExceptionHandler(value = {Exception.class, RuntimeException.class})public Result<String> exceptionHandler(Exception e) {e.printStackTrace();log.error("运行时发生异常:{}", e.getMessage());return Result.failure(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());}@ExceptionHandler(AsyncRequestTimeoutException.class)public Result<String> handException(AsyncRequestTimeoutException e) {e.printStackTrace();log.error("运行时超时异常:{}", e.getMessage());return Result.failure(HttpStatus.BAD_REQUEST.value(), e.getMessage());}
}

一般来说控制器可以这样用

@RestController
@RequestMapping("lang")
public class LangController {@RequestMapping("get")public Result get(){return Result.success(LocalUtil.get("demo"));}
}

三、全局返回处理(装逼用的)

还是上面的全局异常处理,不过要增加点东西

  • 实现ResponseBodyAdvice接口
  • 实现两个方法
    完整代码如下:
@RestControllerAdvice
@Slf4j
public class ControllerExceptionHandel implements ResponseBodyAdvice<Object>{@Autowiredprivate ObjectMapper objectMapper;@ExceptionHandler(ValidationException.class)public Result<String> handleException(ValidationException e) {e.printStackTrace();log.error("参数校验发生异常:{}", e.getMessage());return Result.failure(HttpStatus.BAD_REQUEST.value(), e.getMessage());}@ExceptionHandler(value = NullPointerException.class)public Result<String> exceptionHandler(NullPointerException e) {e.printStackTrace();log.error("空指针异常:{}", e.getMessage());return Result.failure(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());}@ExceptionHandler(value = {Exception.class, RuntimeException.class})public Result<String> exceptionHandler(Exception e) {e.printStackTrace();log.error("运行时发生异常:{}", e.getMessage());return Result.failure(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());}@ExceptionHandler(AsyncRequestTimeoutException.class)public Result<String> handException(AsyncRequestTimeoutException e) {e.printStackTrace();log.error("运行时超时异常:{}", e.getMessage());return Result.failure(HttpStatus.BAD_REQUEST.value(), e.getMessage());}@Overridepublic boolean supports(MethodParameter returnType, Class converterType) {return true;}@SneakyThrows@Overridepublic Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {Class<?> returnClass = returnType.getMethod().getReturnType();if (body instanceof String || Objects.equals(returnClass, String.class)) {return objectMapper.writeValueAsString(Result.success(body));}if (body instanceof Result) {return body;}return Result.success(body);}
}

这样控制器中既可以像之前那样使用,还可以像如下使用
它会自动封装返回结果,无需手动封装

@RestController
@RequestMapping("lang")
public class LangController {@RequestMapping("get")public String get(){return LocalUtil.get("demo");}
}

总结

如果项目中按照这种方式 @RestControllerAdvice + ResponseBodyAdvice接口,就能实现大部分的统一返回出参功能了,无论是正常请求还是异常请求,无论是简单类型还是集合返回,相当于都有了统一的封装处理;
快试试,用到项目中吧~

http://www.hkea.cn/news/548040/

相关文章:

  • 践行新使命忠诚保大庆网站建设线上广告
  • 定制网站建设服务商商家联盟营销方案
  • 集团官网建设公司外贸seo推广公司
  • 佛山新网站制作平台网站诊断工具
  • 做PPT的网站canvawhois查询
  • 营销型网站建设吉林定制化网站建设
  • 个人网上公司注册流程图新站优化案例
  • 做se要明白网站明星百度指数排名
  • 网页微博草稿箱在哪西安seo推广优化
  • 嘉兴微信网站建设谷歌首页
  • 什么网站做海报b站不收费网站
  • 如何自己做个简单网站seo知识点
  • 有哪些做批发的网站有哪些手续百度推广优化是什么意思
  • 用阿里巴巴店铺做公司网站怎么样引擎搜索有哪些
  • 网页制作软件属于什么软件类别简述seo的优化流程
  • 网站建设 公司新闻谷歌排名网站优化
  • 怎样做自己的vip解析网站佛山外贸seo
  • 我的网站在百度搜不到了seo是什么职业做什么的
  • 网站私信界面国外网站seo免费
  • wordpress mysql类惠州网站seo
  • 为什么做网站必须要用域名举出最新的网络营销的案例
  • 电子请柬网站开发百度竞价推广登录入口
  • 网站设计与推广国际时事新闻2022最新
  • 柬埔寨网站开发营销技巧和营销方法
  • 网站建立价格长沙网站外包公司
  • 王建设医生个人网站免费google账号注册入口
  • 免费自建手机网站搜索引擎优化的方法包括
  • 甘肃省建设工程安全质量监督管理局网站官网拉新项目官方一手平台
  • 做电影网站赚钱武汉新闻最新消息
  • 做网站没有成本的方法上海百度分公司电话