一、RestTemplate
请求方法使用
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Object... uriVariables) throws RestClientException { Type type = responseType.getType(); RequestCallback requestCallback = httpEntityCallback(requestEntity, type); ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(type); return nonNull(execute(url, method, requestCallback, responseExtractor, uriVariables)); }1、RestTemplate请求添加header
HttpEntity<?> requestEntity参数中可添加header BaseSAPResponse<SAPResponseData<SAPShopInfo>> response = restTemplate.exchange(getShopCompanyInfoUrl+filter, HttpMethod.GET, new HttpEntity<String>(headers), new ParameterizedTypeReference<BaseSAPResponse<SAPResponseData<SAPShopInfo>>>() { }).getBody();2、URL拼参数
可直接拼参数,即使拼接的参数中有空格也可以,如:&$filter=externalCode eq '123'
如果使用apache包下的HttpClient,拼接的参数中有空格就不可以。
二、@FeignClient
方式1:
@FeignClient(url = "https://api15.sapsf.cn/odata/v2", name = "sap", fallbackFactory = SapSfRemoteHystrix.class) public interface SapSfRemote { /** * 获取门店主体公司名字 * @return */ @RequestMapping(path = "/cust_Shop?$format=json&$expand=cust_legalNav&$select=externalCode,externalName,cust_legalNav/name,cust_legalNav/externalCode", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) BaseSAPResponse<SAPResponseData<SAPShopInfo>> queryShopBelongCompany(@RequestParam("$filter") String filter, @RequestHeader("Authorization") String authorization); }方式2:
@FeignClient(name = "shop-employee", fallback = EmployeeRemoteHystrix.class) public interface EmployeeRemote { /** * 根据根据门店编号和条件查询门店技师列表 * * @param shopId * @param statusList * @param notStatusList * @return */ @GetMapping(value = "/openApi/query/queryEmployeeListByShopId") BizBaseResponse<EmployeesListResponse> queryEmployeeListByShopIdGet(@RequestParam("shopId") Integer shopId, @RequestParam("statusList") List<Integer> statusList, @RequestParam("notStatusList") List<Integer> notStatusList); }