Spring?RestTemplate如何利用攔截器打印請(qǐng)求參數(shù)和返回狀態(tài)
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è)參考,也希望大家多多支持腳本之家。
- Springboot中攔截GET請(qǐng)求獲取請(qǐng)求參數(shù)驗(yàn)證合法性核心方法
- SpringMVC打印請(qǐng)求參數(shù)和響應(yīng)數(shù)據(jù)最優(yōu)方案
- springmvc請(qǐng)求轉(zhuǎn)發(fā)和重定向問(wèn)題(攜帶參數(shù)和不攜帶參數(shù))
- springboot如何設(shè)置請(qǐng)求參數(shù)長(zhǎng)度和文件大小限制
- 解讀SpringBoot接收List<Bean>參數(shù)問(wèn)題(POST請(qǐng)求方式)
- Spring?MVC實(shí)現(xiàn)GET請(qǐng)求接收Date類型參數(shù)
- Spring請(qǐng)求傳遞參數(shù)的解決方案
相關(guā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-11Mybatis實(shí)現(xiàn)動(dòng)態(tài)SQL編寫的示例詳解
這篇文章主要為大家詳細(xì)介紹了mybatis中的動(dòng)態(tài)sql的使用以及緩存的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下2023-02-02Java 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-09Maven dependencies與dependencyManagement的區(qū)別詳解
這篇文章主要介紹了Maven dependencies與dependencyManagement的區(qū)別詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04JAVA讀取HDFS的文件數(shù)據(jù)出現(xiàn)亂碼的解決方案
這篇文章主要介紹了JAVA讀取HDFS的文件數(shù)據(jù)出現(xiàn)亂碼的解決方案,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-11-11eclipse+myeclipse 環(huán)境配置方法
eclipse+myeclipse配置環(huán)境2009-07-07Spring實(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-07Java使用RedisTemplate操作Redis遇到的坑
這篇文章主要介紹了Java使用RedisTemplate操作Redis遇到的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12