您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“SpringCloud gateway怎么修改返回?cái)?shù)據(jù)”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
開源軟件 | 版本 |
---|---|
springboot | 2.1.6.RELEASE |
jdk | 11.0.3 |
主要引入了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}" } }
定義路由及過濾器 test-route路由,當(dāng)Path為/users時(shí),轉(zhuǎn)到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
yaml中配置的filter名字,加“GatewayFilterFactory”,就是對(duì)應(yīng)的過濾器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 原數(shù)據(jù)類型,可以指定為具體數(shù)據(jù)類型,我這里指定為Object,是為了處理多種數(shù)據(jù)類型。 // 當(dāng)然支持多接口返回多數(shù)據(jù)類型的統(tǒng)一修改,yaml中的配置,path,uri需要做相關(guān)調(diào)整 // outClass 目標(biāo)數(shù)據(jù)類型 // rewriteFunction 內(nèi)容重寫方法 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()); } }
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怎么修改返回?cái)?shù)據(jù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。