restTemplate之exchange调用

restTemplate配置

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        RestTemplate restTemplate = new RestTemplate(factory);
        // 支持中文编码
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(Charset.forName("UTF-8")));
        return restTemplate;
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(60000);//单位为ms
        factory.setConnectTimeout(60000);//单位为ms
        return factory;
    }

}

封装调用

@Component
public class HttpUtils {

    private static Logger logger = LoggerFactory.getLogger(HttpUtils.class);

    @Autowired
    private RestTemplate restTemplate;

    private static HttpUtils httpUtil;

    @PostConstruct
    public void init(){
        httpUtil = this;
        httpUtil.restTemplate = this.restTemplate;
    }

    /**
     * 携带json请求体的请求
     * @return
     */
    public static String jsonRequest(String url, HttpMethod method, HttpHeaders headers, Map<String, Object> requestBody){
        try{
            if (headers == null){
                headers = new HttpHeaders();
            }
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            // 封装请求头和请求体,注意json一定要转化未String,否则有可能出现调用失败问题
            HttpEntity entity = new HttpEntity<>(JSON.toJSONString(requestBody), headers);
            logger.info(String.format("RPC远程调用-入参, url: %s, method: %s, entity: %s",url, method.name(), JSON.toJSONString(entity)));
            ResponseEntity<String> result = httpUtil.restTemplate.exchange(url, method, entity, String.class);
            logger.info(String.format("RPC远程调用-出参, url: %s, method: %s, result: %s",url, method.name(),result.getBody()));
            return result.getBody();
        }catch (Exception e){
            logger.error(String.format("RPC远程调用-异常, 异常信息: %s",e.getMessage()));
        }
        return StringUtils.EMPTY;
    }

    /**
     * 携带json数组的请求
     */
    public static <T> String jsonRequest(String url, HttpMethod method, HttpHeaders headers, List<T> list){
        try{
            if (headers == null){
                headers = new HttpHeaders();
            }
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            // 封装请求头和请求体,注意json一定要转化未String,否则有可能出现调用失败问题
            HttpEntity entity = new HttpEntity<>(JSON.toJSONString(list), headers);
            logger.info(String.format("RPC远程调用-入参, url: %s, method: %s, entity: %s",url, method.name(), JSON.toJSONString(entity)));
            ResponseEntity<String> result = httpUtil.restTemplate.exchange(url, method, entity, String.class);
            logger.info(String.format("RPC远程调用-出参, url: %s, method: %s, result: %s",url, method.name(),result.getBody()));
            return result.getBody();
        }catch (Exception e){
            logger.error(String.format("RPC远程调用-异常, 异常信息: %s",e.getMessage()));
        }
        return StringUtils.EMPTY;
    }

    /**
     * 携带form表单请求
     */
    public static String formRequest(String url,HttpMethod method, HttpHeaders headers, Map<String, Object> params){
        try{
            if (headers == null){
                headers = new HttpHeaders();
            }
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            // 封装请求头和请求体
            HttpEntity entity = new HttpEntity<>(null, headers);
            logger.info(String.format("RPC远程调用-入参, url: %s, entity: %s, params: %s",url, JSON.toJSONString(entity), JSON.toJSONString(params)));
            ResponseEntity<String> result = httpUtil.restTemplate.exchange(url,method,entity,String.class,params);
            logger.info(String.format("RPC远程调用-出参, url: %s, entity: %s, params: %s, result: %s",url, JSON.toJSONString(entity),JSON.toJSONString(params), result.getBody()));
            return result.getBody();
        }catch (Exception e){
            logger.error(String.format("RPC远程调用-异常, 异常信息: %s",e.getMessage()));
        }
        return StringUtils.EMPTY;
    }

}

你可能感兴趣的:(java,restful,http)