package com.script.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.Map;
@Slf4j
public class HttpUtils {
private static SimpleClientHttpRequestFactory requestFactory;
private final static String DEFAULT_CONTENT_TYPE="application/x-www-form-urlencoded";
private final static String MULTIPART_CONTENT_TYPE="multipart/form-data;charset=utf-8";
private static RestTemplate restTemplate;
private static HttpUtils httpUtil;
// @Autowired
// private RestTemplate restTemplate;
static {
requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(60000); // 连接超时时间,单位=毫秒
requestFactory.setReadTimeout(60000); // 读取超时时间,单位=毫秒
httpUtil = new HttpUtils();
restTemplate = new RestTemplate(requestFactory);
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
fastConverter.setDefaultCharset(Charset.forName("UTF-8"));
HttpMessageConverter<?> converter = fastConverter;
restTemplate.getMessageConverters().add(converter);
// restTemplate.getMessageConverters().forEach(x -> {
// System.out.println(x);
// x.getSupportedMediaTypes().forEach(System.out::println);
// });
}
private HttpUtils() {
}
public static HttpUtils getInstance(){
return httpUtil;
}
public <T> T post(String url, Map<String, String> reqHeader, Object body, Class<T> T) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
if(reqHeader != null){
for (String key : reqHeader.keySet()) {
headers.add(key, reqHeader.get(key));
}
}
HttpEntity<String> httpEntity = new HttpEntity<>(JSONObject.toJSON(body).toString(), headers);
log.info("post--{}",JSONObject.toJSON(body).toString());
return restTemplate.postForObject(url, httpEntity, T);
}
public <T> T get(String url, MultiValueMap<String,String> params, Class<T> T){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
URI uri = builder.queryParams(params).build().encode().toUri();
return restTemplate.getForObject(uri,T);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
httpUtil.zip (4个子文件)
目录结构.png 27KB
TestHttp.java 1KB
HttpUtils.java 3KB
RestTemplateConfig.java 2KB
共 4 条
- 1
资源评论
攻城狮狮
- 粉丝: 622
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功