http请求调用RestTemplate和@FeignClient使用心得

一、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);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值