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

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

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

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

最近在項(xiàng)目中遇到用RestTemplate請(qǐng)求另外一個(gè)服務(wù)接口,發(fā)現(xiàn)總是報(bào)400返回。或許由于對(duì)400錯(cuò)誤不是很了解,調(diào)試了很久。

但是過(guò)了好一段時(shí)間,發(fā)現(xiàn)自己進(jìn)展不大,由此,咨詢下了經(jīng)驗(yàn)豐富的人,也解決了RestTemplate請(qǐng)求另外服務(wù)接口的方法。

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

?@Autowired
? ? private RestTemplate restTemplate;

但是在請(qǐng)求的時(shí)候,發(fā)現(xiàn)總是返回400.應(yīng)該是參數(shù)問(wèn)題,然后就采用別人幫忙寫的一個(gè)類,去檢查自己請(qǐng)求參數(shù)是否完整,返回參數(shù),定義一個(gè)類

LoggingClientHttpRequestInterceptor去實(shí)現(xiàn)

ClientHttpRequestInterceptor

代碼結(jié)構(gòu)如下:

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=================================================");
? ? }
}

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

重新在配置類中定義一個(gè)bean

代碼如下:

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

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

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

攔截器中通過(guò)response返回JSON數(shù)據(jù)

做接口的攔截器時(shí),需在攔截器中通過(guò)response返回接口是否允許調(diào)用的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;
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot整合Jasypt實(shí)現(xiàn)配置加密的步驟詳解

    SpringBoot整合Jasypt實(shí)現(xiàn)配置加密的步驟詳解

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

    Mybatis實(shí)現(xiàn)動(dòng)態(tài)SQL編寫的示例詳解

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

    Java was started but returned exit code=13問(wèn)題解決案例詳解

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

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

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

    JAVA讀取HDFS的文件數(shù)據(jù)出現(xiàn)亂碼的解決方案

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

    jsp中EL表達(dá)式獲取數(shù)據(jù)

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

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

    eclipse+myeclipse配置環(huán)境
    2009-07-07
  • Spring實(shí)現(xiàn)定時(shí)任務(wù)的幾種方式總結(jié)

    Spring實(shí)現(xiàn)定時(shí)任務(wù)的幾種方式總結(jié)

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

    Java使用RedisTemplate操作Redis遇到的坑

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

    Java for循環(huán)詳解

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

最新評(píng)論