最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringCloud項目中Feign組件添加請求頭所遇到的坑及解決

 更新時間:2023年04月26日 10:47:44   作者:沙礫  
這篇文章主要介紹了SpringCloud項目中Feign組件添加請求頭所遇到的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

前言

在spring cloud的項目中用到了feign組件,簡單配置過后即可完成請求的調(diào)用。

又因為有向請求添加Header頭的需求,查閱了官方示例后,就覺得很簡單,然后一頓操作之后調(diào)試報錯...

按官方修改的示例:

#MidServerClient.java
import feign.Param;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(value = "edu-mid-server")
public interface MidServerClient {

    @RequestMapping(value = "/test/header", method = RequestMethod.GET)
    @Headers({"userInfo:{userInfo}"})
    Object headerTest(@Param("userInfo") String userInfo);
}

提示錯誤:

java.lang.IllegalArgumentException: method GET must not have a request body.

分析

通過斷點debug發(fā)現(xiàn)feign發(fā)請求時把userInfo參數(shù)當成了requestBody來處理,而okhttp3會檢測get請求不允許有body(其他類型的請求哪怕不報錯,但因為不是設置到請求頭,依然不滿足需求)。

查閱官方文檔里是通過Contract(Feign.Contract.Default)來解析注解的:

Feign annotations define the Contract between the interface and how the underlying client should work. Feign's default contract defines the following annotations:

AnnotationInterface TargetUsage
@RequestLineMethodDefines the HttpMethod and UriTemplate for request. Expressions, values wrapped in curly-braces {expression} are resolved using their corresponding @Param annotated parameters.
@ParamParameterDefines a template variable, whose value will be used to resolve the corresponding template Expression, by name.
@HeadersMethod, TypeDefines a HeaderTemplate; a variation on a UriTemplate. that uses @Param annotated values to resolve the corresponding Expressions. When used on a Type, the template will be applied to every request. When used on a Method, the template will apply only to the annotated method.
@QueryMapParameterDefines a Map of name-value pairs, or POJO, to expand into a query string.
@HeaderMapParameterDefines a Map of name-value pairs, to expand into Http Headers
@BodyMethodDefines a Template, similar to a UriTemplate and HeaderTemplate, that uses @Param annotated values to resolve the corresponding Expressions.

從自動配置類找到使用的是spring cloud的SpringMvcContract(用來解析@RequestMapping相關(guān)的注解),而這個注解并不會處理解析上面列的注解

@Configuration
public class FeignClientsConfiguration {
	···
	@Bean
	@ConditionalOnMissingBean
	public Contract feignContract(ConversionService feignConversionService) {
		return new SpringMvcContract(this.parameterProcessors, feignConversionService);
	}
	···

解決

原因找到了

spring cloud使用了自己的SpringMvcContract來解析注解,導致默認的注解解析方式失效。

解決方案自然就是重新解析處理feign的注解,這里通過自定義Contract繼承SpringMvcContract再把Feign.Contract.Default解析邏輯般過來即可(重載的方法是在SpringMvcContract基礎(chǔ)上做進一步解析,否則Feign對RequestMapping相關(guān)對注解解析會失效)

代碼如下(此處只對@Headers、@Param重新做了解析):

#FeignCustomContract.java

import feign.Headers;
import feign.MethodMetadata;
import feign.Param;
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor;
import org.springframework.cloud.openfeign.support.SpringMvcContract;
import org.springframework.core.convert.ConversionService;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.*;

import static feign.Util.checkState;
import static feign.Util.emptyToNull;

public class FeignCustomContract extends SpringMvcContract {


    public FeignCustomContract(List<AnnotatedParameterProcessor> annotatedParameterProcessors, ConversionService conversionService) {
        super(annotatedParameterProcessors, conversionService);
    }

    @Override
    protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodAnnotation, Method method) {
        //解析mvc的注解
        super.processAnnotationOnMethod(data, methodAnnotation, method);
        //解析feign的headers注解
        Class<? extends Annotation> annotationType = methodAnnotation.annotationType();
        if (annotationType == Headers.class) {
            String[] headersOnMethod = Headers.class.cast(methodAnnotation).value();
            checkState(headersOnMethod.length > 0, "Headers annotation was empty on method %s.", method.getName());
            data.template().headers(toMap(headersOnMethod));
        }
    }

    @Override
    protected boolean processAnnotationsOnParameter(MethodMetadata data, Annotation[] annotations, int paramIndex) {
        boolean isMvcHttpAnnotation = super.processAnnotationsOnParameter(data, annotations, paramIndex);

        boolean isFeignHttpAnnotation = false;
        for (Annotation annotation : annotations) {
            Class<? extends Annotation> annotationType = annotation.annotationType();
            if (annotationType == Param.class) {
                Param paramAnnotation = (Param) annotation;
                String name = paramAnnotation.value();
                checkState(emptyToNull(name) != null, "Param annotation was empty on param %s.", paramIndex);
                nameParam(data, name, paramIndex);
                isFeignHttpAnnotation = true;
                if (!data.template().hasRequestVariable(name)) {
                    data.formParams().add(name);
                }
            }
        }
        return isMvcHttpAnnotation || isFeignHttpAnnotation;

    }

    private static Map<String, Collection<String>> toMap(String[] input) {
        Map<String, Collection<String>> result =
                new LinkedHashMap<String, Collection<String>>(input.length);
        for (String header : input) {
            int colon = header.indexOf(':');
            String name = header.substring(0, colon);
            if (!result.containsKey(name)) {
                result.put(name, new ArrayList<String>(1));
            }
            result.get(name).add(header.substring(colon + 1).trim());
        }
        return result;
    }
}
#FeignCustomConfiguration.java

import feign.Contract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.ConversionService;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class FeignCustomConfiguration {

    ···
    @Autowired(required = false)
    private List<AnnotatedParameterProcessor> parameterProcessors = new ArrayList<>();

    @Bean
    @ConditionalOnProperty(name = "feign.feign-custom-contract", havingValue = "true", matchIfMissing = true)
    public Contract feignContract(ConversionService feignConversionService) {
        return new FeignCustomContract(this.parameterProcessors, feignConversionService);
    }
    ···

改完馬上進行新一頓的操作, 看請求日志已經(jīng)設置成功,響應OK!:

請求:{"type":"OKHTTP_REQ","uri":"/test/header","httpMethod":"GET","header":"{"accept":["/"],"userinfo":["{"userId":"sssss","phone":"13544445678],"x-b3-parentspanid":["e49c55484f6c19af"],"x-b3-sampled":["0"],"x-b3-spanid":["1d131b4ccd08d964"],"x-b3-traceid":["9405ce71a13d8289"]}","param":""}

響應{"type":"OKHTTP_RESP","uri":"/test/header","respStatus":0,"status":200,"time":5,"header":"{"cache-control":["no-cache,no-store,max-age=0,must-revalidate"],"connection":["keep-alive"],"content-length":["191"],"content-type":["application/json;charset=UTF-8"],"date":["Fri,11Oct201913:02:41GMT"],"expires":["0"],"pragma":["no-cache"],"x-content-type-options":["nosniff"],"x-frame-options":["DENY"],"x-xss-protection":["1;mode=block"]}"}

feign官方鏈接

spring cloud feign 鏈接

(另一種實現(xiàn)請求頭的方式:實現(xiàn)RequestInterceptor,但是存在hystrix線程切換的坑)

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java-Java5.0注解全面解讀

    Java-Java5.0注解全面解讀

    這篇文章主要介紹了Java-Java5.0注解全面解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java身份證合法性校驗工具類實例代碼

    java身份證合法性校驗工具類實例代碼

    這篇文章主要給大家介紹了關(guān)于java身份證合法性校驗工具類的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • Spring Boot配置過濾器的2種方式示例

    Spring Boot配置過濾器的2種方式示例

    這篇文章主要給大家介紹了關(guān)于Spring Boot配置過濾器的2種方式,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-09-09
  • SpringCloud Webflux過濾器增加header傳遞方式

    SpringCloud Webflux過濾器增加header傳遞方式

    這篇文章主要介紹了SpringCloud Webflux過濾器增加header傳遞方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 基于JavaMail的常用類詳細介紹

    基于JavaMail的常用類詳細介紹

    以下是對JavaMail的常用類進行了詳細分析的介紹,需要的朋友可以過來參考下
    2013-09-09
  • Java多線程工具篇BlockingQueue的詳解

    Java多線程工具篇BlockingQueue的詳解

    今天小編就為大家分享一篇關(guān)于Java多線程工具篇BlockingQueue的詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • java實現(xiàn)發(fā)牌小程序

    java實現(xiàn)發(fā)牌小程序

    這篇文章主要為大家詳細介紹了java實現(xiàn)發(fā)牌小程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Java/Android 實現(xiàn)簡單的HTTP服務器

    Java/Android 實現(xiàn)簡單的HTTP服務器

    這篇文章主要介紹了Java/Android 如何實現(xiàn)簡單的HTTP服務器,幫助大家更好的進行功能測試,感興趣的朋友可以了解下
    2020-10-10
  • Java實現(xiàn)讀取TXT和CSV文件內(nèi)容

    Java實現(xiàn)讀取TXT和CSV文件內(nèi)容

    這篇文章主要為大家詳細介紹了如何利用Java語言實現(xiàn)讀取TXT和CSV文件內(nèi)容的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-02-02
  • java8列表中通過stream流根據(jù)對象屬性去重的三種方式

    java8列表中通過stream流根據(jù)對象屬性去重的三種方式

    這篇文章主要介紹了java8列表中通過stream流根據(jù)對象屬性去重的三種方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08

最新評論

彩票| 双牌县| 县级市| 塔河县| 台江县| 高清| 静安区| 高安市| 大同县| 德阳市| 民县| 雅江县| 垣曲县| 南召县| 勐海县| 和顺县| 万源市| 施甸县| 甘德县| 虎林市| 泰宁县| 察哈| 镇坪县| 崇礼县| 万安县| 明溪县| 察雅县| 肥城市| 涿鹿县| 内江市| 安国市| 忻州市| 闸北区| 湄潭县| 宜川县| 巩义市| 莱州市| 德阳市| 合作市| 洞头县| 保康县|