欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?RestTemplate如何利用攔截器打印請求參數和返回狀態(tài)

 更新時間:2023年07月10日 08:35:17   作者:追夢的搬運工  
這篇文章主要介紹了Spring?RestTemplate如何利用攔截器打印請求參數和返回狀態(tài)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Spring RestTemplate利用攔截器打印請求參數和返回狀態(tài)

最近在項目中遇到用RestTemplate請求另外一個服務接口,發(fā)現總是報400返回?;蛟S由于對400錯誤不是很了解,調試了很久。

但是過了好一段時間,發(fā)現自己進展不大,由此,咨詢下了經驗豐富的人,也解決了RestTemplate請求另外服務接口的方法。

很多人都基本用Spring注入的RestTemplate,代碼如下:

?@Autowired
? ? private RestTemplate restTemplate;

但是在請求的時候,發(fā)現總是返回400.應該是參數問題,然后就采用別人幫忙寫的一個類,去檢查自己請求參數是否完整,返回參數,定義一個類

LoggingClientHttpRequestInterceptor去實現

ClientHttpRequestInterceptor

代碼結構如下:

public class LoggingClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
? ? private final static Logger LOGGER = LoggerFactory.getLogger(LoggingClientHttpRequestInterceptor.class);
? ? @Override
? ? public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
? ? ? ? traceRequest(request, body);
? ? ? ? ClientHttpResponse response = execution.execute(request, body);
? ? ? ? traceResponse(response);
? ? ? ? return response;
? ? }
? ? private void traceRequest(HttpRequest request, byte[] body) throws IOException {
? ? ? ? LOGGER.debug("===========================request begin================================================");
? ? ? ? LOGGER.debug("URI ? ? ? ? : {}", request.getURI());
? ? ? ? LOGGER.debug("Method ? ? ?: {}", request.getMethod());
? ? ? ? LOGGER.debug("Headers ? ? : {}", request.getHeaders());
? ? ? ? LOGGER.debug("Request body: {}", new String(body, "UTF-8"));
? ? ? ? LOGGER.debug("==========================request end================================================");
? ? }
? ? private void traceResponse(ClientHttpResponse response) throws IOException {
? ? ? ? StringBuilder inputStringBuilder = new StringBuilder();
? ? ? ? try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody(), "UTF-8"))) {
? ? ? ? ? ? String line = bufferedReader.readLine();
? ? ? ? ? ? while (line != null) {
? ? ? ? ? ? ? ? inputStringBuilder.append(line);
? ? ? ? ? ? ? ? inputStringBuilder.append('\n');
? ? ? ? ? ? ? ? line = bufferedReader.readLine();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? LOGGER.debug("============================response begin==========================================");
? ? ? ? LOGGER.debug("Status code ?: {}", response.getStatusCode());
? ? ? ? LOGGER.debug("Status text ?: {}", response.getStatusText());
? ? ? ? LOGGER.debug("Headers ? ? ?: {}", response.getHeaders());
? ? ? ? LOGGER.debug("Response body: {}", inputStringBuilder.toString());//WARNING: comment out in production to improve performance
? ? ? ? LOGGER.debug("=======================response end=================================================");
? ? }
}

很多人一看此類,其實都覺得簡單,但是在實際中很大作用。定義好此類后,想法,將這個攔截器添加到restTempate中,這里有兩種方法,

重新在配置類中定義一個bean

代碼如下:

@Bean
? public RestTemplate restTemplate() {
? ? RestTemplate restTemplate = new RestTemplate();
? ? restTemplate.setInterceptors(Collections.singletonList(loggingClientHttpRequestInterceptor));
? ? return restTemplate;
? }

可以直接用此方法加入攔截器

restTemplate.getInterceptors().add(new LoggingClientHttpRequestInterceptor());

攔截器中通過response返回JSON數據

做接口的攔截器時,需在攔截器中通過response返回接口是否允許調用的JSON信息:

response.setCharacterEncoding("UTF-8");  
response.setContentType("application/json; charset=utf-8");
PrintWriter out = null ;
try{
    JSONObject res = new JSONObject();
    res.put("success","false");
    res.put("msg","xxxx");
    out = response.getWriter();
    out.append(res.toString());
    return false;
}
catch (Excepton e){
    e.printStackTrace();
    response.sendError(500);
    return false;
}

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • SpringBoot整合Jasypt實現配置加密的步驟詳解

    SpringBoot整合Jasypt實現配置加密的步驟詳解

    Jasypt是一個Java庫,提供了一種簡單的加密解密方式,可用于保護敏感數據,例如密碼、API密鑰和數據庫連接信息等,本文給大家介紹了SpringBoot整合Jasypt實現配置加密的詳細步驟,感興趣的同學可以參考一下
    2023-11-11
  • Mybatis實現動態(tài)SQL編寫的示例詳解

    Mybatis實現動態(tài)SQL編寫的示例詳解

    這篇文章主要為大家詳細介紹了mybatis中的動態(tài)sql的使用以及緩存的相關知識,文中的示例代碼講解詳細,具有一定的學習價值,需要的可以參考一下
    2023-02-02
  • Java was started but returned exit code=13問題解決案例詳解

    Java was started but returned exit code=13問題解決案例詳解

    這篇文章主要介紹了Java was started but returned exit code=13問題解決案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-09-09
  • Maven dependencies與dependencyManagement的區(qū)別詳解

    Maven dependencies與dependencyManagement的區(qū)別詳解

    這篇文章主要介紹了Maven dependencies與dependencyManagement的區(qū)別詳解,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04
  • JAVA讀取HDFS的文件數據出現亂碼的解決方案

    JAVA讀取HDFS的文件數據出現亂碼的解決方案

    這篇文章主要介紹了JAVA讀取HDFS的文件數據出現亂碼的解決方案,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-11-11
  • jsp中EL表達式獲取數據

    jsp中EL表達式獲取數據

    EL 全名為Expression Language。本文給大家介紹的是在jsp中EL表達式獲取數據的幾種方式,希望大家能夠喜歡
    2016-07-07
  • eclipse+myeclipse 環(huán)境配置方法

    eclipse+myeclipse 環(huán)境配置方法

    eclipse+myeclipse配置環(huán)境
    2009-07-07
  • Spring實現定時任務的幾種方式總結

    Spring實現定時任務的幾種方式總結

    Spring Task 是 Spring 框架提供的一種任務調度和異步處理的解決方案,可以按照約定的時間自動執(zhí)行某個代碼邏輯它可以幫助開發(fā)者在 Spring 應用中輕松地實現定時任務、異步任務等功能,提高應用的效率和可維護性,需要的朋友可以參考下本文
    2024-07-07
  • Java使用RedisTemplate操作Redis遇到的坑

    Java使用RedisTemplate操作Redis遇到的坑

    這篇文章主要介紹了Java使用RedisTemplate操作Redis遇到的坑,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java for循環(huán)詳解

    Java for循環(huán)詳解

    這篇文章主要介紹了Java for循環(huán)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03

最新評論