转自:
springboot之RestTemplate接口封装的示例分享
下文笔者讲述封装RestTemplate接口的示例分享,如下所示
实现思路:只需对RestTemplate方法进行相应的封装即可实现HttpClient的效果
例:
import java.util.Map;import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate;import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference;public class RestTemplateToInterface {/*** * @param hashMap 请求参数* @param token token验证* @param getOrPost get或者post请求* @param url 请求路径* @return*/public static MapgetData(Map hashMap,String token,String getOrPost,String url) {RestTemplate restTemplate = new RestTemplate();//设置请求头,或其他需要需要的HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.add("Content-Type", "application/json; charset=UTF-8");//设置参数;HttpEntity