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返回?;蛟S由于對(duì)400錯(cuò)誤不是很了解,調(diào)試了很久。
但是過了好一段時(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ù)問題,然后就采用別人幫忙寫的一個(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í)都覺得簡單,但是在實(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());
攔截器中通過response返回JSON數(shù)據(jù)
做接口的攔截器時(shí),需在攔截器中通過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ā)和重定向問題(攜帶參數(shù)和不攜帶參數(shù))
- springboot如何設(shè)置請(qǐng)求參數(shù)長度和文件大小限制
- 解讀SpringBoot接收List<Bean>參數(shù)問題(POST請(qǐng)求方式)
- Spring?MVC實(shí)現(xiàn)GET請(qǐng)求接收Date類型參數(shù)
- Spring請(qǐng)求傳遞參數(shù)的解決方案
相關(guān)文章
Spring+MyBatis多數(shù)據(jù)源配置實(shí)現(xiàn)示例
本篇文章主要介紹了Spring+MyBatis多數(shù)據(jù)源配置實(shí)現(xiàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
SpringBoot Event 事件如何實(shí)現(xiàn)異步延遲執(zhí)行
這篇文章主要介紹了Spring Boot Event 事件如何實(shí)現(xiàn)異步延遲執(zhí)行問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
java sftp下載文件報(bào)錯(cuò)Caused by:com.jcraft.jsch.JSchExcep
文章講述了作者在日常工作中遇到的JSch連接問題,經(jīng)過分析發(fā)現(xiàn)是由于連接泄露導(dǎo)致的,作者提出了解決方案,并給出了使用建議:1.在finally代碼塊中關(guān)閉連接;2.在真正使用階段再創(chuàng)建連接,避免創(chuàng)建后不使用又忘記關(guān)閉連接2024-11-11
JAVA泛型的繼承和實(shí)現(xiàn)、擦除原理解析
這篇文章主要介紹了JAVA泛型的繼承和實(shí)現(xiàn)、擦除原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
SpringBoot如何使用ApplicationContext獲取bean對(duì)象
這篇文章主要介紹了SpringBoot 如何使用ApplicationContext獲取bean對(duì)象,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot項(xiàng)目創(chuàng)建使用+配置文件+日志文件詳解
Spring的出現(xiàn)是為了簡化 Java 程序開發(fā),而 SpringBoot 的出現(xiàn)是為了簡化 Spring 程序開發(fā),這篇文章主要介紹了SpringBoot項(xiàng)目創(chuàng)建使用+配置文件+日志文件,需要的朋友可以參考下2023-02-02
Go Java算法之外觀數(shù)列實(shí)現(xiàn)方法示例詳解
這篇文章主要為大家介紹了Go Java算法外觀數(shù)列實(shí)現(xiàn)的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

