温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringCloud gateway怎么修改返回数据

发布时间:2021-06-30 17:31:21 来源:亿速云 阅读:629 作者:chen 栏目:大数据

本篇内容介绍了“SpringCloud gateway怎么修改返回数据”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

SpringCloud gateway 修改返回数据

版本说明

开源软件版本
springboot2.1.6.RELEASE
jdk11.0.3

gradle

主要引入了springboot 2.1,lombok

plugins {
    id 'org.springframework.boot' version '2.1.6.RELEASE'
    id 'java'
    id "io.freefair.lombok" version "3.6.6"
}

apply plugin: 'io.spring.dependency-management'

group = 'cn.buddie.demo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "Greenwich.SR2")
}

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
    compile 'org.projectlombok:lombok:1.18.8'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

yaml

定义路由及过滤器 test-route路由,当Path为/users时,转到uri中配置的链接http://127.0.0.1:8123/users中。使用UnionResult来过滤

spring:
  cloud:
    gateway:
      enabled: true
      routes:
      - id: test-route
        uri: http://127.0.0.1:8123/users
        predicates:
        - Path=/users
        filters:
        - UnionResult

filter

yaml中配置的filter名字,加“GatewayFilterFactory”,就是对应的过滤器Java类

package cn.buddie.demo.springcloudgateway.filter;

import cn.buddie.demo.springcloudgateway.model.UnionResult;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.rewrite.ModifyResponseBodyGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.rewrite.RewriteFunction;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * description
 *
 * @author buddie.wei
 * @date 2019/7/20
 */
@Component
public class UnionResultGatewayFilterFactory extends ModifyResponseBodyGatewayFilterFactory {
    @Override
    public GatewayFilter apply(Config config) {
        return new ModifyResponseGatewayFilter(this.getConfig());
    }


    private Config getConfig() {
        Config cf = new Config();
        // Config.setRewriteFunction(Class<T> inClass, Class<R> outClass, RewriteFunction<T, R> rewriteFunction)
        // inClass 原数据类型,可以指定为具体数据类型,我这里指定为Object,是为了处理多种数据类型。
        //                      当然支持多接口返回多数据类型的统一修改,yaml中的配置,path,uri需要做相关调整
        // outClass 目标数据类型
        // rewriteFunction 内容重写方法
        cf.setRewriteFunction(Object.class, UnionResult.class, getRewriteFunction());
        return cf;
    }

    private RewriteFunction<Object, UnionResult> getRewriteFunction() {
        return (exchange, resp) -> Mono.just(UnionResult.builder().requestId(exchange.getRequest().getHeaders().getFirst("cn-buddie.demo.requestId")).result(resp).build());
    }
}

model

package cn.buddie.demo.springcloudgateway.model;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

/**
 * description
 *
 * @author buddie.wei
 * @date 2019/7/20
 */
@Getter
@Setter
@Builder
public class UnionResult {
    private String requestId;
    private Object result;
}

“SpringCloud gateway怎么修改返回数据”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI