当前位置: 首页 > 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/412038/

相关文章:

  • 国发网站建设西安做网站公司
  • 网站推广服务合同简述网络营销的主要方法
  • 信息门户网站是什么成人计算机培训机构哪个最好
  • 网站建设公司 中企动力公司东莞商城网站建设
  • b2c的电子商务网站自己想做个网站怎么做
  • 京东pc网站用什么做的如何注册网站怎么注册
  • 长沙商城网站制作seo线下培训课程
  • web网站开发公司网站制作优化排名
  • 这么做3d网站企业邮箱网页版
  • 瑞安网站建设公司关键词排名网络推广
  • 南京学做网站友情链接检查工具
  • 参考文献网站开发百度重庆营销中心
  • 如何做微信ppt模板下载网站企业网页设计公司
  • 做b2b网站百度点击快速排名
  • 网站怎么做移动图片不显示不出来吗芭嘞seo
  • 旅游网站建设服务器ip域名解析
  • 企业网站建设三个原则百度指数资讯指数是指什么
  • 房地产集团网站建设方案软文文案案例
  • 阜蒙县建设学校网站是什么北京seo编辑
  • 珠海建设局网站十大经典事件营销案例分析
  • 创建网站开发公司互联网推广引流是做什么的
  • 万盛集团网站建设seo网站推广全程实例
  • 做教育的网站需要资质吗网站怎么开发
  • 微网站怎么做滚动中国万网域名注册官网
  • 个人如何免费建网站seo在线优化工具 si
  • 双线主机可以做彩票网站吗网络推广合作协议
  • 做外贸的b2b网站域名批量查询系统
  • 建设网站需要哪些职位网站建设策划书
  • 苏州网站建设哪里好网站点击排名优化
  • 网站建设收费标准策划百度推广关键词越多越好吗