Feign client 设置请求头信息

Feign client端

@FeignClient(url = "${test.url}", name = "cclient",configuration= ClientConfiguration.class,fallback = APIClientFallback.class)
public interface APIClient {
	
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/check/test")
	String checkResult(@RequestParam("sendTelNo") String sendTelNo,@RequestParam("certType") String certType,@RequestParam("certCode") String certCode,@RequestParam("userName") String userName);
	

	
	@RequestMapping(method = RequestMethod.POST, value = "/userstaus/test")
	String inusetime(@RequestParam("sendTelNo") String sendTelNo);
	
	

	@RequestMapping(method = RequestMethod.POST, value = "/userstaus/test")
	String offnetIdentify(@RequestParam("sendTelNo") String sendTelNo,@RequestParam("date") String date);
	
	

配置文件 application-dev.yml

test:
      url: https://xxxxxx:8243/test
      tokenId: 11111112222222 

feign configuration 这里配置全局的请求头和 token

@Configuration
public class ClientConfiguration {
	
	@Value("${test.tokenId}")
	private String tokenId;
	
	@Bean
    public RequestInterceptor headerInterceptor() {
		return new RequestInterceptor(){

			@Override
			public void apply(RequestTemplate template) {
				List<String> authorizationList = Lists.newArrayList("Bearer "+tokenId);
				List<String> contentTypeList = Lists.newArrayList("application/x-www-form-urlencoded;charset=utf-8");
				Map<String, Collection<String>> headers =ImmutableMap.of("Authorization", authorizationList,"Content-Type", contentTypeList);
				template.headers(headers);
			}
		};
	}

feign 异常处理

@Component
public class APIClientFallback implements APIClient{

	@Override
	public String checkResult(String sendTelNo, String certType, String certCode, String userName) {
		return toJsonString();
	}

	@Override
	public String inusetime(String sendTelNo) {
		return toJsonString();
	}

	@Override
	public String offnetIdentify(String sendTelNo, String date) {
		return toJsonString();
	}

	private String toJsonString() {
		BaseResult resultVo = new BaseResult();
		resultVo.renderStatus(ResultTypeEnum.SERVICE_ERROR);
		ObjectMapper mapper = new ObjectMapper();
		try {
			return mapper.writeValueAsString(resultVo);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}

}

转载于:https://my.oschina.net/penghaozhong/blog/1550003

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值