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;
}
}