本文为博主原创,未经允许不得转载:
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 = LoggerFactory.getLogger(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, param.toString(), 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请求并返回 return restTemplate.getForObject(url, responseType); } @Override public String getForObject(String url, HttpHeaders headers) { // 设置ContentType headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // 设置请求头 HttpEntity<String> requestEntity = new HttpEntity<>(null, headers); // get请求获取响应内容 ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class); // 返回响应body return response.getBody(); } @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 = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class); // 返回响应body LOGGER.info(url + "&&&&&" + response.toString() + "&&&&&" + response.getBody()); return response.getBody(); } }
标签: spring