@ConTrollerAdvice的使用

@ConTrollerAdvice,從名字上面看是控制器增強的意思。

在javaDoc寫到
/**
* Indicates the annotated class assists a "Controller".
*
* <p>Serves as a specialization of {@link Component @Component}, allowing for
* implementation classes to be autodetected through classpath scanning.
*
* <p>It is typically used to define {@link ExceptionHandler @ExceptionHandler},
* {@link InitBinder @InitBinder}, and {@link ModelAttribute @ModelAttribute}
* methods that apply to all {@link RequestMapping @RequestMapping} methods.
*
* @author Rossen Stoyanchev
* @since 3.2
*/
也就是把@ControllerAdvice注解內部所有的@ExceptionHandler,@ InitBinderhe @ ModelAttribute應用到所有的@RequestMapping注解的方法里面去。
比較常用的場景,可以將異常處理器應用到所有的控制器中,而不是當個的控制器。
@RestControllerAdvice
public class ExecptionHanlerController {

@ExceptionHandler(value=Exception.class)
public String handleException(Exception e){

return "不要慌張,系統出現故障,請聯系管理員!"+e.getMessage();

}

@ModelAttribute
public void addAttribute(Model model){
model.addAttribute("mess", "world");
}

}

@RestController
public class HelloController {

@RequestMapping("/home")
public String home(@ModelAttribute("mess")String message){

return "hello!"+message;

}

@RequestMapping("/ex")
public String getexception(){
throw new IllegalArgumentException("真的不知道說什么.....");
}
}

啟動String Boot就可以訪問了。

轉載于:https://www.cnblogs.com/yimixiong/p/7291824.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391795.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391795.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391795.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Mybatis—注解開發

Mybatis的注解開發 MyBatis的常用注解 這幾年來注解開發越來越流行&#xff0c;Mybatis也可以使用注解開發方式&#xff0c;這樣我們就可以減少編寫Mapper映射文件了。 Insert&#xff1a;實現新增 Update&#xff1a;實現更新 Delete&#xff1a;實現刪除 Select&#x…

道路工程結構計算軟件_我從軟件工程到產品管理的道路

道路工程結構計算軟件by Sari Harrison莎莉哈里森(Sari Harrison) 我從軟件工程到產品管理的道路 (My path from software engineering to product management) 以及一些有關如何自己做的建議 (And some advice on how to do it yourself) I am often asked how to make the m…

Vue 指令

下面列舉VUE的HTML頁面模板指令&#xff0c;并進行分別練習。 1. templates 2. v-if, v-for <div idapp><ol><li v-for"todo in todos>{{ todo.text}}</li></ol> </div><script>app new Vue({ el: #app, data: { return…

iOS-FMDB

2019獨角獸企業重金招聘Python工程師標準>>> #import <Foundation/Foundation.h> #import <FMDatabase.h> #import "MyModel.h"interface FMDBManager : NSObject {FMDatabase *_dataBase; }(instancetype)shareInstance;- (BOOL)insert:(MyM…

解決朋友圈壓縮_朋友中最有趣的朋友[已解決]

解決朋友圈壓縮We live in uncertain times.我們生活在不確定的時代。 We don’t know when we’re going back to school or the office. We don’t know when we’ll be able to sit inside at a restaurant. We don’t even know when we’ll be able to mosh at a Korn co…

西安項目分析

西安物流 西安高考補習 西安藝考 轉載于:https://www.cnblogs.com/wpxuexi/p/7294269.html

MapServer應用開發平臺示例

MapServer為當前開源WebGIS的應用代表&#xff0c;在西方社會應用面極為廣泛&#xff0c;現介紹幾個基于它的開源應用平臺。 1.GeoMOOSE GeoMoose is a Web Client Javascript Framework for displaying distributed cartographic data. Among its many strengths, it can hand…

leetcode 995. K 連續位的最小翻轉次數(貪心算法)

在僅包含 0 和 1 的數組 A 中&#xff0c;一次 K 位翻轉包括選擇一個長度為 K 的&#xff08;連續&#xff09;子數組&#xff0c;同時將子數組中的每個 0 更改為 1&#xff0c;而每個 1 更改為 0。 返回所需的 K 位翻轉的最小次數&#xff0c;以便數組沒有值為 0 的元素。如果…

kotlin數據庫_如何在Kotlin應用程序中使用Xodus數據庫

kotlin數據庫I want to show you how to use one of my favorite database choices for Kotlin applications. Namely, Xodus. Why do I like using Xodus for Kotlin applications? Well, here are a couple of its selling points:我想向您展示如何在Kotlin應用程序中使用我…

使用route add添加路由,使兩個網卡同時訪問內外網

route add命令格式&#xff1a;route [-f] [-p] [Command] [Destination] [mask Netmask] [Gateway] [metric Metric] [if Interface] 通過配置電腦的靜態路由來實現同時訪問內外網的。電腦的網絡IP配置不用變&#xff0c;兩個網卡都按照正常配置&#xff08;都配置IP地址、子網…

基于JavaConfig配置的Spring MVC的構建

上一篇講了基于XML配置的構建&#xff0c;這一篇講一講基于JavaConfig的構建。為什么要寫這篇文章&#xff0c;因為基于xml配置的構建&#xff0c;本人認為很麻煩&#xff0c;要寫一堆的配置&#xff0c;不夠簡潔&#xff0c;而基于JavacConfig配置的構建符合程序員的編碼習慣&…

pymc3 貝葉斯線性回歸_使用PyMC3進行貝葉斯媒體混合建模,帶來樂趣和收益

pymc3 貝葉斯線性回歸Michael Johns, Zhenyu Wang, Bruno Dupont, and Luca Fiaschi邁克爾約翰斯&#xff0c;王振宇&#xff0c;布魯諾杜邦和盧卡菲亞斯基 “If you can’t measure it, you can’t manage it, or fix it”“如果無法衡量&#xff0c;就無法管理或修復它” –…

webkit中對incomplete type指針的處理技巧

近日在研究webkit的時候發現了一個函數 template<typename T> inline void deleteOwnedPtr(T* ptr) {typedef char known[sizeof(T) ? 1 : -1];if(sizeof(known))delete ptr; } 一開始對這個函數非常費解&#xff0c;為什么作者不直接 delete ptr; 通過上stackoverflow提…

leetcode 1004. 最大連續1的個數 III(滑動窗口)

給定一個由若干 0 和 1 組成的數組 A&#xff0c;我們最多可以將 K 個值從 0 變成 1 。 返回僅包含 1 的最長&#xff08;連續&#xff09;子數組的長度。 示例 1&#xff1a; 輸入&#xff1a;A [1,1,1,0,0,0,1,1,1,1,0], K 2 輸出&#xff1a;6 解釋&#xff1a; [1,1,1…

我如何找到工作并找到理想的工作

By Julius Zerwick朱利葉斯澤威克(Julius Zerwick) This article is about how I went through my job hunt for a full time position as a software engineer in New York City and ended up with my dream job. I had spent two years building my skills and had aspirati…

synchronized 與 Lock 的那點事

synchronized 與 Lock 的那點事 最近在做一個監控系統&#xff0c;該系統主要包括對數據實時分析和存儲兩個部分&#xff0c;由于并發量比較高&#xff0c;所以不可避免的使用到了一些并發的知識。為了實現這些要求&#xff0c;后臺使用一個隊列作為緩存&#xff0c;對于請求只…

ols線性回歸_普通最小二乘[OLS]方法使用于機器學習的簡單線性回歸變得容易

ols線性回歸Hello Everyone!大家好&#xff01; I am super excited to be writing another article after a long time since my previous article was published.自從上一篇文章發表很長時間以來&#xff0c;我很高興能寫另一篇文章。 A Simple Linear Regression [SLR] is…

ubuntu安裝配置jdk

先去 Oracle下載Linux下的JDK壓縮包&#xff0c;我下載的是jdk-7u4-linux-i586.tar.gz文件&#xff0c;下好后直接解壓Step1:# 將解壓好的jdk1.7.0_04文件夾用最高權限復制到/usr/lib/jvm目錄里sudo cp -r ~/jdk1.7.0_04/ /usr/lib/jvm/Step2:# 配置環境變量sudo gedit ~/.prof…

leetcode 697. 數組的度(hashmap)

給定一個非空且只包含非負數的整數數組 nums&#xff0c;數組的度的定義是指數組里任一元素出現頻數的最大值。 你的任務是在 nums 中找到與 nums 擁有相同大小的度的最短連續子數組&#xff0c;返回其長度。 示例 1&#xff1a; 輸入&#xff1a;[1, 2, 2, 3, 1] 輸出&…

facebook機器學習_如何為您的頁面創建Facebook Messenger機器人

facebook機器學習by Paul Pinard保羅皮納德(Paul Pinard) 如何為您的頁面創建Facebook Messenger機器人 (How to create a Facebook messenger bot for your page) When it comes to sharing your chatbot, Facebook Messenger is a must. We created a very easy step-by-ste…