In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
本篇内容介绍了"SpringCloud gateway怎么修改返回数据"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
SpringCloud gateway 修改返回数据版本说明开源软件版本springboot2.1.6.RELEASEjdk11.0.3gradle
主要引入了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: - UnionResultfilter
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 */@Componentpublic 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 inClass, Class outClass, RewriteFunction rewriteFunction) // inClass 原数据类型,可以指定为具体数据类型,我这里指定为Object,是为了处理多种数据类型。 // 当然支持多接口返回多数据类型的统一修改,yaml中的配置,path,uri需要做相关调整 // outClass 目标数据类型 // rewriteFunction 内容重写方法 cf.setRewriteFunction(Object.class, UnionResult.class, getRewriteFunction()); return cf; } private RewriteFunction getRewriteFunction() { return (exchange, resp) -> Mono.just(UnionResult.builder().requestId(exchange.getRequest().getHeaders().getFirst("cn-buddie.demo.requestId")).result(resp).build()); }}modelpackage cn.buddie.demo.springcloudgateway.model;import lombok.Builder;import lombok.Getter;import lombok.Setter;/** * description * * @author buddie.wei * @date 2019/7/20 */@Getter@Setter@Builderpublic class UnionResult { private String requestId; private Object result;}"SpringCloud gateway怎么修改返回数据"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.