1. 实现方式一
直接写在方法请求注解PostMapping上:
@PostMapping(headers = {"X-GW-AccessKey=iAfRg0FqH5yS7BQYY6pAh0jLxJoWoa6b"})
package com.ennova.tour.search.integration.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.net.URI;
import java.util.LinkedHashMap;
/**
* 搜索平台接口
*
* @author zrj
* @since 2021/8/2
**/
@Service("SearchService")
@FeignClient(url = "https://rdfa-gateway.ennew.com/search-api/", name = "SearchService")
public interface SearchPlatformService {
/**
* 新增数据
*/
@PostMapping(headers = {
"X-GW-AccessKey=iAfRg0FqH5yS7BQYY6pAh0jLxJoWoa6b"})
LinkedHashMap insertDoc(URI uri, @RequestBody LinkedHashMap linkedHashMap);
/**
* 搜索数据
*/
@PostMapping(headers = {
"X-GW-AccessKey=iAfRg0FqH5yS7BQYY6pAh0jLxJoWoa6b"})
LinkedHashMap selectDoc(URI uri, @RequestBody LinkedHashMap linkedHashMap);
}
2. 实现方式二
通过Feign配置类feign configuration 实现全局的请求头和 token设置,相当于每次feign请求都会自动带上这些头信息。
/**
* 客户端设置头信息
*
* @author zrj
* @since 2021/11/11
**/
@Slf4j
@Configuration
public class ClientConfiguration {
@Value("${tour.search.headers}")
private String headers;
@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);
template.header("X-GW-AccessKey", headers);
}
};
}
}
/**
* 搜索平台接口
*
* @author zrj
* @since 2021/8/2
**/
@Service("SearchService")
@FeignClient(url = "https://rdfa-gateway.ennew.com/search-api/", name = "SearchService")
public interface SearchPlatformService {
/**
* 新增数据
*/
@PostMapping
LinkedHashMap insertDoc(URI uri, @RequestBody LinkedHashMap linkedHashMap);
/**
* 搜索数据
*/
@PostMapping
LinkedHashMap selectDoc(URI uri, @RequestBody LinkedHashMap linkedHashMap);
}