作者:O依楼观雪O | 来源:互联网 | 2023-09-24 19:33
新增配置packagecom.zhy.viewer.config;importfeign.RequestInterceptor;importfeign.RequestTempl
新增配置
package com.zhy.viewer.config;import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Objects;/*** feign转发header** @author zhy*/
@Configuration
@Slf4j
public class FeignConfiguration implements RequestInterceptor {@Overridepublic void apply(RequestTemplate template) {ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();Enumeration headerNames = request.getHeaderNames();if (headerNames != null) {while (headerNames.hasMoreElements()) {String name = headerNames.nextElement();String values = request.getHeader(name);template.header(name, values);}log.info("feign interceptor header:{}", template);}}
}
调用的地方
@FeignClient(name = "zhy-ai", configuration = FeignConfiguration.class)
public interface CtFeignClientService {/*** 提交*/@PostMapping(value = "/zhy/api/submit")void viewerSubmitAlgorithm(@RequestBody ViewerDto viewerDto);}