springrestTemplate进⾏http请求的⼯具类封装本⽂为博主原创,未经允许不得转载:
1.对常⽤调⽤的⽅法进⾏封装:
import org.springframework.http.HttpHeaders;
import com.alibaba.fastjson.JSONObject;
public interface SpringRestService {
public <T> T postWithJson(String url, JSONObject param, Class<T> responseType);
public <T> T postWithJson(String url, HttpHeaders headers, JSONObject param, Class<T> responseType);
public <T> T postWithJson(String url, HttpHeaders headers, String param, Class<T> responseType);
public <T> T getForObject(String url, Class<T> responseType);
public String getForObject(String url, HttpHeaders headers);
public String getForXml(String url, HttpHeaders headers);
}
2.对上⾯接⼝实现封装:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONObject;
import com.intf.service.springrest.SpringRestService;
@Service("springRestService")
public class SpringRestServiceImpl implements SpringRestService {
@Autowired
private RestTemplate restTemplate;
private static final Logger LOGGER = Logger(SpringRestServiceImpl.class);
@Override
public <T> T postWithJson(String url, JSONObject param, Class<T> responseType) {
// 设置请求头
HttpHeaders headers = new HttpHeaders();
// 设置ContentType
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
// 请求
return postWithJson(url, headers, param, responseType);
}
@Override
public <T> T postWithJson(String url, HttpHeaders headers, JSONObject param, Class<T> responseType) {
// 请求
return postWithJson(url, headers, String(), responseType);
}
@Override
public <T> T postWithJson(String url, HttpHeaders headers, String param, Class<T> responseType) {
// 设置ContentType
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
// 请求中设置param和headers
HttpEntity<String> request = new HttpEntity<>(param, headers);
// post请求并返回
return restTemplate.postForObject(url, request, responseType);
}
@Override
public <T> T getForObject(String url, Class<T> responseType) {
/
/ get请求并返回
ForObject(url, responseType);
}
@Override
public String getForObject(String url, HttpHeaders headers) {
// 设置ContentType
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
// 设置请求头spring framework版本
HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
// get请求获取响应内容
ResponseEntity<String> response = hange(url, HttpMethod.GET, requestEntity, String.class);
// 返回响应body
Body();
}
@Override
public String getForXml(String url, HttpHeaders headers) {
// 设置ACCEPT
headers.add("Accept", MediaType.APPLICATION_XML_VALUE);
// 设置ContentType
headers.setContentType(MediaType.APPLICATION_XML);
// 设置请求头
HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
/
/ get请求获取响应内容
ResponseEntity<String> response = hange(url, HttpMethod.GET, requestEntity, String.class);
// 返回响应body
LOGGER.info(url + "&&&&&" + String() + "&&&&&" + Body());
Body();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论