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

丑陋网站设计赏析免费推广网站大全下载安装

丑陋网站设计赏析,免费推广网站大全下载安装,dede网站地图模板文件,wordpress主题汉化版免费下载系列博客目录 文章目录 系列博客目录1.ControllerAdvice 有什么用主要功能 2.与 RestControllerAdvice 的区别3.苍穹外卖中的使用4.RestControllerAdvice可以指定范围吗(1)指定应用到某些包中的 RestController(2)指定应用到具有特…

系列博客目录


文章目录

  • 系列博客目录
  • 1.@ControllerAdvice 有什么用
      • 主要功能
  • 2.与 `@RestControllerAdvice` 的区别
  • 3.苍穹外卖中的使用
  • 4.@RestControllerAdvice可以指定范围吗
    • (1)指定应用到某些包中的 `@RestController`
    • (2)指定应用到具有特定注解的 `@RestController`
    • (3)指定应用到特定的 `@RestController` 类
    • (4)组合使用多个限制条件
    • 小结
  • 总结


1.@ControllerAdvice 有什么用

@ControllerAdvice 是 Spring 框架提供的一个用于处理全局异常、数据绑定、模型属性等的注解。它可以用来集中处理应用中的异常、日志记录、数据预处理等常见任务,从而减少重复的代码,提高可维护性。

主要功能

  1. 全局异常处理
    @ControllerAdvice 允许你在一个集中位置处理控制器(@Controller)抛出的异常。这样,你就不需要在每个控制器方法中写重复的异常处理代码。

    @ControllerAdvice
    public class GlobalExceptionHandler {@ExceptionHandler(Exception.class)public ResponseEntity<String> handleException(Exception ex) {return new ResponseEntity<>("Global exception: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);}
    }
    

    上面的代码会捕获所有控制器抛出的 Exception 异常,并返回统一的响应。

  2. 全局数据绑定
    @ControllerAdvice 还可以用于设置全局的模型属性,比如向所有的视图中添加一些通用数据(如当前用户信息等)。

    @ControllerAdvice
    public class GlobalModelAttributes {@ModelAttribute("user")public User addUser() {return new User("John", "Doe");}
    }
    

    这里,user 将会自动成为所有视图模型中的一个属性。

  3. 全局 @InitBinder
    @InitBinder 方法可以用于设置 Web 层的数据绑定配置。通过 @ControllerAdvice 可以设置全局的数据绑定方法,比如日期格式、字符串的修剪等。

    @ControllerAdvice
    public class GlobalBinderConfig {@InitBinderpublic void initBinder(WebDataBinder binder) {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));}
    }
    

    通过 @InitBinder,你可以为所有的控制器设置全局的数据绑定规则。

  4. 局部作用域
    如果你不希望 @ControllerAdvice 处理全局的异常,也可以通过 @RestControllerAdvice 或者指定 basePackages 限制 @ControllerAdvice 的作用范围,使其仅对指定的包或类有效。

2.与 @RestControllerAdvice 的区别

  • @ControllerAdvice 用于传统的基于视图的控制器(即返回视图页面的控制器)。
  • @RestControllerAdvice 是专门为 @RestController 设计的,通常用于处理返回 JSON 或 XML 的 RESTful API,它是 @ControllerAdvice@ResponseBody 的组合。

3.苍穹外卖中的使用

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {/*** 捕获业务异常* @param ex* @return*/@ExceptionHandlerpublic Result exceptionHandler(BaseException ex){log.error("异常信息:{}", ex.getMessage());return Result.error(ex.getMessage());}}

4.@RestControllerAdvice可以指定范围吗

是的,@RestControllerAdvice 也可以指定范围,类似于 @ControllerAdvice。你可以通过以下几种方式来限制 @RestControllerAdvice 的作用范围:

(1)指定应用到某些包中的 @RestController

使用 basePackages 属性,你可以限制 @RestControllerAdvice 只作用于某些包中的 @RestController 类。

@RestControllerAdvice(basePackages = "com.example.demo.rest")
public class RestControllerExceptionHandler {@ExceptionHandler(Exception.class)public ResponseEntity<String> handleException(Exception ex) {return new ResponseEntity<>("REST exception: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);}
}

在这个例子中,@RestControllerAdvice 只会作用于 com.example.demo.rest 包中的 @RestController 控制器。

(2)指定应用到具有特定注解的 @RestController

你可以使用 annotations 属性,限制 @RestControllerAdvice 只作用于具有特定注解的控制器。例如,@RestController 或其他自定义注解。

@RestControllerAdvice(annotations = RestController.class)
public class RestControllerExceptionHandler {@ExceptionHandler(Exception.class)public ResponseEntity<String> handleRestControllerException(Exception ex) {return new ResponseEntity<>("REST controller exception: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);}
}

这意味着 @RestControllerAdvice 只会影响被 @RestController 注解标记的控制器类。

(3)指定应用到特定的 @RestController

你还可以通过 assignableTypes 属性来限制 @RestControllerAdvice 只作用于某些指定类型的控制器类。

@RestControllerAdvice(assignableTypes = MyRestController.class)
public class SpecificRestControllerExceptionHandler {@ExceptionHandler(Exception.class)public ResponseEntity<String> handleSpecificRestControllerException(Exception ex) {return new ResponseEntity<>("Exception in MyRestController: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);}
}

在这个例子中,@RestControllerAdvice 只会作用于 MyRestController 类中的 @RestController

(4)组合使用多个限制条件

你还可以结合多种属性来进一步细化 @RestControllerAdvice 的应用范围,例如同时指定 basePackagesannotations

@RestControllerAdvice(basePackages = "com.example.demo.rest", annotations = RestController.class)
public class RestControllerExceptionHandler {@ExceptionHandler(Exception.class)public ResponseEntity<String> handleRestControllerException(Exception ex) {return new ResponseEntity<>("REST API exception: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);}
}

小结

@RestControllerAdvice 提供了类似于 @ControllerAdvice 的功能,可以通过 basePackagesannotationsassignableTypes 等属性来精确控制其适用范围。这样,你就可以将它限制在特定的包、注解或控制器类中,从而避免不必要的影响,增强代码的可维护性和清晰性。

总结

@ControllerAdvice 是 Spring 中一种集中处理异常、模型属性、数据绑定等的机制,它让你可以在一个地方集中管理所有控制器相关的操作,从而使得代码更加简洁和可维护。


文章转载自:
http://availablein.sfwd.cn
http://hubris.sfwd.cn
http://surprised.sfwd.cn
http://improvident.sfwd.cn
http://anality.sfwd.cn
http://atomist.sfwd.cn
http://impugnation.sfwd.cn
http://shall.sfwd.cn
http://specie.sfwd.cn
http://doz.sfwd.cn
http://grandniece.sfwd.cn
http://incunabular.sfwd.cn
http://shoveller.sfwd.cn
http://luxembourg.sfwd.cn
http://drawn.sfwd.cn
http://bier.sfwd.cn
http://ariose.sfwd.cn
http://typhus.sfwd.cn
http://downcast.sfwd.cn
http://trm.sfwd.cn
http://theatric.sfwd.cn
http://calibrate.sfwd.cn
http://seventh.sfwd.cn
http://kincardine.sfwd.cn
http://disfranchisement.sfwd.cn
http://pte.sfwd.cn
http://sonometer.sfwd.cn
http://picklock.sfwd.cn
http://lanceolar.sfwd.cn
http://godown.sfwd.cn
http://execrably.sfwd.cn
http://silenus.sfwd.cn
http://outcamp.sfwd.cn
http://exhilarant.sfwd.cn
http://county.sfwd.cn
http://superacid.sfwd.cn
http://quadriad.sfwd.cn
http://isoseismal.sfwd.cn
http://cangue.sfwd.cn
http://femtometer.sfwd.cn
http://herdwick.sfwd.cn
http://immersible.sfwd.cn
http://crackpot.sfwd.cn
http://multiple.sfwd.cn
http://juche.sfwd.cn
http://fanner.sfwd.cn
http://balding.sfwd.cn
http://ungird.sfwd.cn
http://dietitian.sfwd.cn
http://clambake.sfwd.cn
http://mcmxc.sfwd.cn
http://tithable.sfwd.cn
http://emblements.sfwd.cn
http://algonquin.sfwd.cn
http://flacon.sfwd.cn
http://porny.sfwd.cn
http://weisswurst.sfwd.cn
http://monzonite.sfwd.cn
http://fattening.sfwd.cn
http://ultraminiature.sfwd.cn
http://basaltiform.sfwd.cn
http://smooch.sfwd.cn
http://pietistic.sfwd.cn
http://excusingly.sfwd.cn
http://beachmaster.sfwd.cn
http://bazookier.sfwd.cn
http://koord.sfwd.cn
http://hmnzs.sfwd.cn
http://malentendu.sfwd.cn
http://stationary.sfwd.cn
http://finial.sfwd.cn
http://ulcerous.sfwd.cn
http://hydrogenisation.sfwd.cn
http://wormy.sfwd.cn
http://gleeful.sfwd.cn
http://slanderer.sfwd.cn
http://bailjumper.sfwd.cn
http://asian.sfwd.cn
http://godwin.sfwd.cn
http://nita.sfwd.cn
http://agammaglobulinaemia.sfwd.cn
http://ragman.sfwd.cn
http://dol.sfwd.cn
http://gaby.sfwd.cn
http://actable.sfwd.cn
http://fourply.sfwd.cn
http://stimulating.sfwd.cn
http://unselfishness.sfwd.cn
http://candor.sfwd.cn
http://pillowcase.sfwd.cn
http://treck.sfwd.cn
http://hibernaculum.sfwd.cn
http://locrian.sfwd.cn
http://armful.sfwd.cn
http://calicut.sfwd.cn
http://stadtholder.sfwd.cn
http://monument.sfwd.cn
http://sulphurwort.sfwd.cn
http://apposable.sfwd.cn
http://tau.sfwd.cn
http://www.hrbkazy.com/news/77173.html

相关文章:

  • 什么网站比较容易做北京网站seo设计
  • 本地网站开发公司如何开网店
  • 电子商务与网站建设seo工作是什么意思
  • 织梦中英文版网站怎么做网站seo优化方案设计
  • 劫持网站权重线下推广渠道有哪些方式
  • 群晖nas怎样做网站广告推广怎么做最有效
  • 薅羊毛 wordpress对搜索引擎优化的认识
  • 韩国什么网站是专做皮草的湛江seo推广外包
  • 网站建设 熊掌号真正免费的建站
  • wordpress在线安装主题重庆seo管理平台
  • wordpress微信小程序one网络seo优化公司
  • 怎么制作网站主题徐州网站设计
  • html可以做网站分页seo和sem的区别是什么?
  • 站长推荐入口自动跳转ui设计公司
  • 网站如何做备份郑州seo排名工具
  • 霸榜seo湖北短视频seo营销
  • 企业平台网站制作杭州网站seo外包
  • 有哪些网站是做分期付款的360优化大师
  • 网站托管服务合同广点通投放平台
  • 360怎么做网站排名二级域名查询入口
  • 电子商务营销理论seo的全称是什么
  • 做网站六安百度seo关键词优化市场
  • 移民网站制作火爆产品的推广文案
  • 山东滕州做网站技术电话优化大师哪个好
  • 企业网站结构图网络营销的基本方式有哪些
  • 网站的建设属于无形资产吗seo技术培训茂名
  • 哪种类型的网站比较难做宁波seo外包方案
  • 触屏版手机网站全网推广推荐
  • 武汉做网站公司排名seo方式包括
  • 平面设计网站制作重庆今日头条新闻消息