RestTemplate 非200请求 响应获取的方法


方法一:

响应是非200请求会抛异常,可以用try去捕获

      try{
            sinGnUpOne_Method(req,token,skuurl);
//            获取200响应
//            {"id":6,"path":"/free/xxxx","reserve":0}
            System.out.println("看下抛异常走不走");
        }catch(Exception e) {
            System.out.println(e.getCause());
//            获取非200的响应
//            400 : [{"error_code":"22222@您已存在订单"}]
            System.out.println(e.getMessage()); ----拿到的是String类型
            System.out.println("走catch");
 

方法二:

try{
            sinGnUpOne_Method(req,token,skuurl);
//            获取200响应
//            {"id":6,"path":"/free/xxxx","reserve":0}
            System.out.println("看下抛异常走不走");
        }catch(HttpClientErrorException e) {
String resBody = e.getResponseBodyAsString(); 
String code = e.getStatusCode().toString(); 
//{"error_code":"22222@您已存在订单"}
System.out.println(resBody); 
//400 BAD_REQUEST
System.out.println(code); 
}

方法三: 

实现ResponseErrorHandler重写,非200也正常返回

实现自定义RestTemplateResponseErrorHandler

RestTemplateResponseErrorHandler

package com.Common.restTemplate;

import com.sun.jersey.api.NotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.ResponseErrorHandler;

import java.io.IOException;

@Component
public class RestTemplateResponseErrorHandler implements ResponseErrorHandler {

    @Override
    public boolean hasError(ClientHttpResponse httpResponse)throws IOException {
        return (httpResponse.getStatusCode().series() == HttpStatus.Series.CLIENT_ERROR
                || httpResponse.getStatusCode().series() == HttpStatus.Series.SERVER_ERROR);
    }

    @Override
    public void handleError(ClientHttpResponse httpResponse)throws IOException {
        if (httpResponse.getStatusCode().series() == HttpStatus.Series.SERVER_ERROR) {
            throw new HttpClientErrorException(httpResponse.getStatusCode());
        } else if (httpResponse.getStatusCode().series() == HttpStatus.Series.CLIENT_ERROR) {
            if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
                throw new NotFoundException();
            }
        }
    }
}

将ResponseErrorHandler实现注入到RestTemplate实例中

RequestConfig --配置文件

package com.config;

import com.Common.restTemplate.RestTemplateResponseErrorHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;


@Configuration
public class RequestConfig{

    @Bean
    public RestTemplate restTemplate(RestTemplateResponseErrorHandler restTemplateResponseErrorHandler) throws Exception {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setErrorHandler(restTemplateResponseErrorHandler);
        return restTemplate;
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        //读取超时时间设置
        factory.setReadTimeout(5000);
        //连接超时时间设置
        factory.setConnectTimeout(15000);
        return factory;
    }

}

参考:

https://blog.csdn.net/y534560449/article/details/115658680?spm=1035.2023.3001.6557&utm_medium=distribute.pc_relevant_bbs_down_v2.none-task-blog-2~default~OPENSEARCH~Rate-2.pc_relevant_bbs_down_v2_default&depth_1-utm_source=distribute.pc_relevant_bbs_down_v2.none-task-blog-2~default~OPENSEARCH~Rate-2.pc_relevant_bbs_down_v2_default

方法三的文件放置

发送请求相关见 https://www.cnblogs.com/kaibindirver/p/15398815.html