溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring?Cloud?Feign使用對象參數(shù)的方法

發(fā)布時間:2022-02-22 15:12:38 來源:億速云 閱讀:209 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“Spring Cloud Feign使用對象參數(shù)的方法”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Spring Cloud Feign使用對象參數(shù)的方法”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

概述

Spring Cloud Feign 用于微服務(wù)的封裝,通過接口代理的實現(xiàn)方式讓微服務(wù)調(diào)用變得簡單,讓微服務(wù)的使用上如同本地服務(wù)。但是它在傳參方面不是很完美。在使用 Feign 代理 GET 請求時,對于簡單參數(shù)(基本類型、包裝器、字符串)的使用上沒有困難,但是在使用對象傳參時卻無法自動的將對象包含的字段解析出來。

如果你沒耐心看完,直接跳到最后一個標(biāo)題跟著操作就行了。

@RequestBody

對象傳參是很常見的操作,雖然可以通過一個個參數(shù)傳遞來替代,但是那樣就太麻煩了,所以必須解決這個問題。

我在網(wǎng)上看到有人用 @RequestBody 來注解對象參數(shù),我在嘗試后發(fā)現(xiàn)確實可用。這個方案實際使用 body 體裝了參數(shù)(使用的是 GET 請求),但是這個方案有些問題:

  1. 注解需要在 consumer 和 provider 兩邊都有,這造成了麻煩

  2. 使用接口測試工具 Postman 無法跑通微服務(wù),后來發(fā)現(xiàn)是因為 body 體的格式選擇不正確,這個格式不是通常的表單或者路徑拼接,而是 GraphQL。我沒有研究過這種格式應(yīng)該如何填寫參數(shù),但是 Postman 上并沒有給出像表單那樣方便的格式,這對于測試是很不利的。

@SpringQueryMap

于是我繼續(xù)尋找答案,發(fā)現(xiàn)可以使用 @SpringQueryMap 僅添加在 consumer 的參數(shù)上就能自動對 Map 類型參數(shù)編碼再拼接到 URL 上。而我用的高版本的 Feign,可以直接把對象編碼。

可是正當(dāng)我以為得到正解時,卻發(fā)現(xiàn)還是有問題:

我明明在 Date 類型的字段上加上了 @DateTimeFormat(pattern = "yyyy-MM-dd"),卻沒有生效,他用自己的方式進(jìn)行了編碼(或者說序列化),而且官方確實沒有提供這種格式化方式。

又一番找尋后發(fā)現(xiàn)了一位大佬自己實現(xiàn)了一個注解轉(zhuǎn)換替代 @SpringQueryMap,并實現(xiàn)了豐富的格式化功能 ORZ,只能說佩服佩服。但是我沒有那樣的技術(shù),又不太想復(fù)制粘貼他那一大堆的代碼,因為出了問題也不好改,所以我還是想堅持最大限度地使用框架,最小限度的給框架填坑。

QueryMapEncoder

終于功夫不費有心人,我發(fā)現(xiàn)了 Feign 預(yù)留的自定義編碼器接口 QueryMapEncoder,框架提供了兩個實現(xiàn):

  • FieldQueryMapEncoder

  • BeanQueryMapEncoder

雖然這兩個實現(xiàn)不能滿足我的要求,但是只要稍加修改寫一個自己的實現(xiàn)類就行了,于是我在 FieldQueryMapEncoder 的基礎(chǔ)上修改,僅僅添加了一個方法,小改了一個方法就實現(xiàn)了功能。

原理:Feign 其實還是用 Map<String, Object> 進(jìn)行的編碼,編碼方式也很簡單,String 是 key,Object 是 value。最開始的方式就是用 Object 的 toString() 方法把參數(shù)編碼,這也是為什么 Date 字段會變成一個默認(rèn)的時間格式,因為 toString() 根本和 @DateTimeFormat 沒有關(guān)系。而高版本使用編碼器實現(xiàn)了對象傳參,實際實際上是通過簡單的反射獲取對象的元數(shù)據(jù),再放到 Map 中。

上面的原理都能從 @DateTimeFormat 的注釋和編碼器的源碼中得到答案。

我們要做的就是自定義一個編碼器,實現(xiàn)在元數(shù)據(jù)放入 Map 之前根據(jù)需要把字段變成我們想要的字符串。下面是我實現(xiàn)的代碼,供參考:

package com.example.billmanagerfront.config.encoder;

import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import org.springframework.format.annotation.DateTimeFormat;
import feign.Param;
import feign.QueryMapEncoder;
import feign.codec.EncodeException;
public class PowerfulQueryMapEncoder implements QueryMapEncoder {
    private final Map<Class<?>, ObjectParamMetadata> classToMetadata = new ConcurrentHashMap<>();
    @Override
    public Map<String, Object> encode(Object object) throws EncodeException {
        ObjectParamMetadata metadata = classToMetadata.computeIfAbsent(object.getClass(),
                ObjectParamMetadata::parseObjectType);
        return metadata.objectFields.stream()
                .map(field -> this.FieldValuePair(object, field))
                .filter(fieldObjectPair -> fieldObjectPair.right.isPresent())
                .collect(Collectors.toMap(this::fieldName, this::fieldObject));
    }
    private String fieldName(Pair<Field, Optional<Object>> pair) {
        Param alias = pair.left.getAnnotation(Param.class);
        return alias != null ? alias.value() : pair.left.getName();
    // 可擴(kuò)展為策略模式,支持更多的格式轉(zhuǎn)換
    private Object fieldObject(Pair<Field, Optional<Object>> pair) {
        Object fieldObject = pair.right.get();
        DateTimeFormat dateTimeFormat = pair.left.getAnnotation(DateTimeFormat.class);
        if (dateTimeFormat != null) {
            DateFormat format = new SimpleDateFormat(dateTimeFormat.pattern());
            format.setTimeZone(TimeZone.getTimeZone("GMT+8")); // TODO: 最好不要寫死時區(qū)
            fieldObject = format.format(fieldObject);
        } else {
        }
        return fieldObject;
    private Pair<Field, Optional<Object>> FieldValuePair(Object object, Field field) {
        try {
            return Pair.pair(field, Optional.ofNullable(field.get(object)));
        } catch (IllegalAccessException e) {
            throw new EncodeException("Failure encoding object into query map", e);
    private static class ObjectParamMetadata {
        private final List<Field> objectFields;
        private ObjectParamMetadata(List<Field> objectFields) {
            this.objectFields = Collections.unmodifiableList(objectFields);
        private static ObjectParamMetadata parseObjectType(Class<?> type) {
            List<Field> allFields = new ArrayList<Field>();
            for (Class<?> currentClass = type; currentClass != null; currentClass = currentClass.getSuperclass()) {
                Collections.addAll(allFields, currentClass.getDeclaredFields());
            }
            return new ObjectParamMetadata(allFields.stream()
                    .filter(field -> !field.isSynthetic())
                    .peek(field -> field.setAccessible(true))
                    .collect(Collectors.toList()));
    private static class Pair<T, U> {
        private Pair(T left, U right) {
            this.right = right;
            this.left = left;
        public final T left;
        public final U right;
        public static <T, U> Pair<T, U> pair(T left, U right) {
            return new Pair<>(left, right);
}

加注釋的方法,就是我后添加進(jìn)去的。encode 方法的最后一行稍微修改了一下,引用了我加的方法,其他都是直接借鑒過來的(本來我想更偷懶,直接繼承一下子,但是它用了私有的內(nèi)部類導(dǎo)致我只能全部復(fù)制粘貼了)。

解決方案

1.不用引入其他的 Feign 依賴,保證有下面這個就行(看網(wǎng)上其他方法還要引入特定依賴,要對應(yīng)版本號,挺麻煩的)

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2.編寫上面那樣的類,你可以直接復(fù)制過去改個包名就行,如果還需要除了 Date 以外的格式化,請看注釋和文章分析。其中我對日期的格式化,直接使用了 @DateTimeFormat 提供的模式,和 Spring 保持了一致。

3.編寫一個 Feign 配置類,將剛自定義的編碼器注冊進(jìn)去。細(xì)節(jié)我就不多說了:

package com.example.billmanagerfront.config;

import com.example.billmanagerfront.config.encoder.PowerfulQueryMapEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Feign;
import feign.Retryer;
@Configuration
public class FeignConfig {
    @Bean
    public Feign.Builder feignBuilder() {
        return Feign.builder()
                .queryMapEncoder(new PowerfulQueryMapEncoder())
                .retryer(Retryer.NEVER_RETRY);
    }
}

4.Feign 代理接口中聲明使用這個配置類,細(xì)節(jié)不談

package com.example.billmanagerfront.client;

import java.util.List;
import com.example.billmanagerfront.config.FeignConfig;
import com.example.billmanagerfront.pojo.Bill;
import com.example.billmanagerfront.pojo.BillType;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "BILL-MANAGER", path = "bill", configuration = FeignConfig.class)
public interface BillClient {
    @GetMapping("list")
    List<Bill> list(@SpringQueryMap(true) Bill b);
    @GetMapping("type")
    List<BillType> type();
    @DeleteMapping("delete/{id}")
    public String delete(@PathVariable("id") Long id);
}

讀到這里,這篇“Spring Cloud Feign使用對象參數(shù)的方法”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI