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

自己怎么做网站购买空间口碑营销的产品

自己怎么做网站购买空间,口碑营销的产品,建立文档,潍坊品牌设计公司在web开发中&#xff0c;规范所有请求响应类型&#xff0c;不管是对前端数据处理&#xff0c;还是后端统一数据解析都是非常重要的。今天我们简单的方式实现如何实现这一效果 实现方式 定义响应类型 public class ResponseResult<T> {private static final String SUC…

在web开发中,规范所有请求响应类型,不管是对前端数据处理,还是后端统一数据解析都是非常重要的。今天我们简单的方式实现如何实现这一效果

实现方式

  1. 定义响应类型

public class ResponseResult<T> {private static final String SUCCESS_CODE = "000";private static final String FAILURE_CODE = "999";private String code;private String message;private T data;public static <T> ResponseResult<T> ok(T data){ResponseResult responseResult = new ResponseResult();responseResult.setCode(SUCCESS_CODE);responseResult.setData(data);return responseResult;}public static ResponseResult fail(String code, String message){if( code == null ){code = FAILURE_CODE;}ResponseResult responseResult = new ResponseResult();responseResult.setCode(code);responseResult.setMessage(message);return responseResult;}public static ResponseResult fail(String message){return fail(FAILURE_CODE, message);}
}
  1. 定义统一的异常处理流程,通过@RestControllerAdvice@ExceptionHandler注解可以对Controller中的异常统一处理

@RestControllerAdvice
public class ControllerAdviceHandle {@ExceptionHandler(Exception.class)public ResponseResult handleException(Exception exception) {BusException busException;if (exception instanceof BusException asException) {busException = asException;} else {busException = convertException(exception);}return ResponseResult.fail(busException.getCode(), busException.getMessage());}
}
  1. 定义统一响应拦截,通过是实现接口ResponseBodyAdvice,这里可以和上面的异常一起处理

public class ControllerAdviceHandle implements ResponseBodyAdvice {@Overridepublic boolean supports(MethodParameter returnType, Class converterType) {return true;}@Overridepublic Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType,ServerHttpRequest request, ServerHttpResponse response) {if( body instanceof ResponseResult){return body;}return ResponseResult.ok(body);}
}
  1. 定义spring配置,实现自动装配

在resource目录添加自动注入配置META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports,这样通过引入jar就可以自动使用该配置

cn.cycad.web.response.ResponseConfig

应用示例

  1. 比如现在有一个User实体,我们通过继承基类

@RestController
@RequestMapping("/test")
public class TestController {@GetMapping("/{val}")public Object get(@PathVariable("val") String val) throws BusException {if( "1".equals(val) ){throw new BusException("参数错误");}return Map.of("val",val);}}
  1. 通过调用请求,可以看到不管是否异常,结果都是下面的格式

{"code": "999","message": null,"data": null
}
http://www.hkea.cn/news/719894/

相关文章:

  • 公司网络营销方案优化设计七年级上册数学答案
  • 网站建设策划方案网址搜索引擎
  • 艺术培训学校系统网站怎么做百度优化是什么
  • 自己的网站做飘窗百度推广账号登录入口
  • 国内好的网站建设国内外十大免费crm软件推荐
  • 淄博品质网站建设百度销售推广
  • 网站建设学习内容网站模板哪家好
  • 建立b2b网站成本微信营销平台系统
  • 学做衣服网 缤纷网站手机百度ai入口
  • 点餐系统网站建设画质优化app下载
  • 上海都有哪些企业公司seo网站seo
  • 进一步加强政府网站建设网站建设介绍ppt
  • 做网站的设计软件上海seo推广外包
  • 中国工程局人才招聘网福建seo推广方案
  • 深圳南山做网站的公司百度投诉中心
  • 辽宁建设工程信息网业绩认定武汉网站优化公司
  • 莱芜都市人才网上海网站seo公司
  • 广州做鞋的网站怎么让某个关键词排名上去
  • 温州平阳县网站建设兼职东莞网络推广哪家公司奿
  • 做单页网站价格微信朋友圈广告在哪里做
  • 濮阳家电网站建设一般开车用什么导航最好
  • html5 图片展示网站大作设计网站
  • 河北正规网站建设比较百度一下你就知道官页
  • 企业网站建设哪家服务好福州网站关键词推广
  • 惠州悦商做网站软件开发一般需要多少钱
  • 做衣服外单网站优化大师官方正版下载
  • 专门做酒店的网站百度排行
  • 上海做手机网站建设盐城网站优化
  • html论坛模板东营seo整站优化
  • 天津网站建设582345网址导航桌面版