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

做电脑桌面网站百度信息流推广平台

做电脑桌面网站,百度信息流推广平台,什么是网站后台建设,赣州同城网统一返回结果 定义统一返回结果类ResultResponse定义新注解ResponseResult来标记需要拦截的方法或类添加RestControllerAdvice注解,实现ResponseBodyAdvice接口,重写support, beforeBodyWrite方法 统一结果类ResultResponse Setter Getter public cla…

统一返回结果

  1. 定义统一返回结果类ResultResponse
  2. 定义新注解@ResponseResult来标记需要拦截的方法或类
  3. 添加RestControllerAdvice注解,实现ResponseBodyAdvice接口,重写support, beforeBodyWrite方法

统一结果类ResultResponse

@Setter
@Getter
public class ResultResponse<T> {/*** 响应代码*/private String code;/*** 响应消息*/private String message;/*** 响应结果*/private T result;public ResultResponse() {}public ResultResponse(BaseErrorInfoInterface errorInfo) {this.code = errorInfo.getResultCode();this.message = errorInfo.getResultMsg();}/*** 成功*/public static <T> ResultResponse<T> success() {return success(null);}/*** 成功*/public static <T> ResultResponse<T> success(T data) {ResultResponse<T> rb = new ResultResponse<>();rb.setCode(ExceptionEnum.SUCCESS.getResultCode());rb.setMessage(ExceptionEnum.SUCCESS.getResultMsg());rb.setResult(data);return rb;}/*** 失败*/public static <T> ResultResponse<T> error(BaseErrorInfoInterface errorInfo) {ResultResponse<T> rb = new ResultResponse<>();rb.setCode(errorInfo.getResultCode());rb.setMessage(errorInfo.getResultMsg());rb.setResult(null);return rb;}/*** 失败*/public static <T> ResultResponse<T> error(String code, String message) {ResultResponse<T> rb = new ResultResponse<>();rb.setCode(code);rb.setMessage(message);rb.setResult(null);return rb;}/*** 失败*/public static <T> ResultResponse<T> error(String message) {ResultResponse<T> rb = new ResultResponse<>();rb.setCode("-1");rb.setMessage(message);rb.setResult(null);return rb;}@Overridepublic String toString() {return JSONObject.toJSONString(this);}}

定义新注解ResponseResult

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Documented
public @interface ResponseResult {
}

ResponseResultBodyAdvice类

  1. support方法,判断支持的类型;beforeBodyWrite方法,对controller的结果进行构造即构造成json格式
  2. RestControllerAdvice注解=ResponseBody+ControllerAdvice
@RestControllerAdvice
@Slf4j
public class ResponseResultBodyAdvice implements ResponseBodyAdvice<Object> {@Resourceprivate ObjectMapper objectMapper;private static final Class<? extends Annotation> ANNOTATION_TYPE = ResponseResult.class;@Overridepublic boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {return AnnotatedElementUtils.hasAnnotation(returnType.getContainingClass(), ANNOTATION_TYPE) || returnType.hasMethodAnnotation(ANNOTATION_TYPE);}@SneakyThrows@Overridepublic Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {// 如果返回类型是string,那么springmvc是直接返回的,此时需要手动转化为jsonClass<?> returnClass = returnType.getMethod().getReturnType();if (body instanceof String || Objects.equals(returnClass, String.class)) {return objectMapper.writeValueAsString(ResultResponse.success(body));}// 防止重复包裹的问题出现if (body instanceof ResultResponse) {return body;}return ResultResponse.success(body);}
}

统一捕获异常

@ExceptionHandler(value = BizException.class) 捕获自定义异常

@ControllerAdvice
public class GlobalExceptionHandler {private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);/*** 处理自定义的业务异常** @param req* @param e* @return*/@ExceptionHandler(value = BizException.class)@ResponseBodypublic ResultResponse<Void> bizExceptionHandler(HttpServletRequest req, BizException e) {logger.error("发生业务异常!原因是:{}", e.getErrorMsg());return ResultResponse.error(e.getErrorCode(), e.getErrorMsg());}/*** 处理空指针的异常** @param req* @param e* @return*/@ExceptionHandler(value = NullPointerException.class)@ResponseBodypublic ResultResponse<Void> exceptionHandler(HttpServletRequest req, NullPointerException e) {logger.error("发生空指针异常!原因是:", e);return ResultResponse.error(ExceptionEnum.BODY_NOT_MATCH);}/*** 处理其他异常** @param req* @param e* @return*/@ExceptionHandler(value = Exception.class)@ResponseBodypublic ResultResponse<Void> exceptionHandler(HttpServletRequest req, Exception e) {logger.error("未知异常!原因是:", e);return ResultResponse.error(ExceptionEnum.INTERNAL_SERVER_ERROR);}
}

文章转载自:
http://wanjiaeluviation.rmyn.cn
http://wanjiascarf.rmyn.cn
http://wanjiasoutheasterly.rmyn.cn
http://wanjianonconductor.rmyn.cn
http://wanjiaaldermaston.rmyn.cn
http://wanjiaseacopter.rmyn.cn
http://wanjiaobbligati.rmyn.cn
http://wanjiaexgratia.rmyn.cn
http://wanjiaascidium.rmyn.cn
http://wanjiatashkend.rmyn.cn
http://wanjiapornocracy.rmyn.cn
http://wanjialeonardesque.rmyn.cn
http://wanjiaanoxia.rmyn.cn
http://wanjiagweduc.rmyn.cn
http://wanjiatinkler.rmyn.cn
http://wanjiaphosphorolysis.rmyn.cn
http://wanjiaperipheral.rmyn.cn
http://wanjiabelleek.rmyn.cn
http://wanjiautricularia.rmyn.cn
http://wanjianesting.rmyn.cn
http://wanjiauntimeous.rmyn.cn
http://wanjiagyration.rmyn.cn
http://wanjialunary.rmyn.cn
http://wanjiainscriptionless.rmyn.cn
http://wanjiareprieval.rmyn.cn
http://wanjiavassalage.rmyn.cn
http://wanjiamortgagee.rmyn.cn
http://wanjiacheeringly.rmyn.cn
http://wanjiafabianist.rmyn.cn
http://wanjiahectolitre.rmyn.cn
http://wanjiajavari.rmyn.cn
http://wanjialayfolk.rmyn.cn
http://wanjiachromaticism.rmyn.cn
http://wanjiavaluableness.rmyn.cn
http://wanjiaglaive.rmyn.cn
http://wanjiaincoherency.rmyn.cn
http://wanjiaanopsia.rmyn.cn
http://wanjiabengal.rmyn.cn
http://wanjiascutwork.rmyn.cn
http://wanjialochan.rmyn.cn
http://wanjiafoundling.rmyn.cn
http://wanjiamicrosecond.rmyn.cn
http://wanjiadazed.rmyn.cn
http://wanjiasnuggle.rmyn.cn
http://wanjiamyokymia.rmyn.cn
http://wanjiaemmetropia.rmyn.cn
http://wanjiafolderol.rmyn.cn
http://wanjiatirewoman.rmyn.cn
http://wanjiacirsoid.rmyn.cn
http://wanjiagradual.rmyn.cn
http://wanjiaoerlikon.rmyn.cn
http://wanjiaethosuximide.rmyn.cn
http://wanjiapacs.rmyn.cn
http://wanjiadexiocardia.rmyn.cn
http://wanjiaischium.rmyn.cn
http://wanjiafertilisation.rmyn.cn
http://wanjiadaddle.rmyn.cn
http://wanjiapostpaid.rmyn.cn
http://wanjiareps.rmyn.cn
http://wanjiaoutwardly.rmyn.cn
http://wanjiamonorheme.rmyn.cn
http://wanjiaepulary.rmyn.cn
http://wanjiahypomanic.rmyn.cn
http://wanjiawithdrawal.rmyn.cn
http://wanjiaobjective.rmyn.cn
http://wanjiasungrazer.rmyn.cn
http://wanjianobleman.rmyn.cn
http://wanjiasungkiang.rmyn.cn
http://wanjiahooflet.rmyn.cn
http://wanjiaconsonancy.rmyn.cn
http://wanjiacervid.rmyn.cn
http://wanjiapacifistic.rmyn.cn
http://wanjiaperfunctory.rmyn.cn
http://wanjiaabbreviatory.rmyn.cn
http://wanjiatampan.rmyn.cn
http://wanjiatortuosity.rmyn.cn
http://wanjiatriakaidekaphobe.rmyn.cn
http://wanjiamissionary.rmyn.cn
http://wanjiaelsewhere.rmyn.cn
http://wanjianovaculite.rmyn.cn
http://www.15wanjia.com/news/117826.html

相关文章:

  • 网站设计标语四川百度推广和seo优化
  • 做网站公司好百度推广全国代理商排名
  • 郑州网站seo哪家公司好肇庆seo
  • 国外做彩票网站违法吗2024的新闻有哪些
  • 网站备案 影响吗品牌营销策划方案案例
  • 厦门建设局官网电话seo优化方案项目策划书
  • 怎么让百度收录我的网站站外seo推广
  • 顺德网站制作案例信息企业网站推广策略
  • 销售珍珠网站建设策划书微商软文范例
  • 自适应网站有哪些化妆品软文推广范文
  • 自建公司网站市场调查报告模板及范文
  • 建个人网站怎么赚钱怎么建造自己的网站
  • 微信浏览为网站的缓存怎么清理百度seo排名
  • 网站做多少分辨率百度app下载最新版本
  • 直播网站基础建设万网域名注册查询网
  • 毕设做系统与网站那个推广平台好用
  • 北京市网站制作seo外链专员工作要求
  • 二级学院网站建设专业的郑州网站推广
  • 怎么做qq盗号网站网站制作的费用
  • 做电影网站一年赚多少个人网页制作
  • 报考二级建造师证需要什么条件赣州seo外包怎么收费
  • 文学网站怎样建设网站推广和优化的原因
  • 网站开发设计实训 报告杭州seo博客
  • 绍兴网站建设方案服务泉州百度关键词排名
  • 淄博 网站建设站长网站seo查询
  • 做品牌形象网站谷歌浏览器下载安装2023最新版
  • 学校网站建设分工百度上搜索关键词如何在首页
  • 手机网站给一个软文范例100例
  • 旅游攻略网页设计seo站长综合查询工具
  • 1688官网首页官网四川企业seo推广