Get請求使用註解校驗失敗

2022-03-26 09:52:12 字數 833 閱讀 1183

今天在get請求上使用註解進行引數校驗,怎麼樣都校驗不到,把解決過程記錄一下。

解決方法 :

在類上面增加 @validated 註解

修改方法訪問許可權為 public (我就栽在這裡了,之前的介面在接手的時候使用的private修飾的)

這時就可以正常使用@notnull 等各種校驗註解了。

需要注意的是這裡校驗失敗時對外拋錯是constraintviolationexception(post請求對實體進行校驗的拋錯是methodargumentnotvalidexception)。

所以需要我們對兩個拋錯都進行處理。

/**

* 處理引數校驗異常

* @param e

* @return

*/@exceptionhandler()

public customizeresponse exception(methodargumentnotvalidexception e)

}return result;

}/**

* 處理引數校驗異常(這個是處理方法校驗的,用於get請求校驗)

* @param e

* @return

*/@exceptionhandler()

public customizeresponse validateexception(constraintviolationexception e)

}return result;

}

使用groovy傳送get請求

def tablename 20180601 引數1 def batchid 0531211200158235 引數2 返回乙個urlconnection物件,它表示到url所引用的遠端物件的連線 在這裡設定一些屬性,詳細見urlconnection文件,httpurlconnection是urlc...

使用requests傳送get請求

基本用法 import requests req requests.get 發起get請求 print req.text 列印響應內容req.ok 檢查返回碼是不是 200 ok 如果是則返回true,否則返回false req.url 檢視請求的url,也就是 req.text 檢視返回的響應內容...

使用httpClient傳送請求get和post

匯入httpclient相關的jar包 2.使用httpclient傳送get請求 public static void main string args throws exception 6.3 獲取響應體 7 釋放資源 3 使用httpclient傳送post請求 public static v...