WebFlux系列(八)WebClient Uri傳參


#Java#Spring#WebFlux#WebClient#Post#Get#傳參#Uri#

WebClient如何通過Uri以Get方式傳參

視頻講解 : https://www.bilibili.com/video/av82953521/

 

WebfluxServerApplication.java
package com.example.webfluxserver;

import lombok.extern.log4j.Log4j2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@Log4j2
@SpringBootApplication
public class WebfluxServerApplication extends BaseApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebfluxServerApplication.class, args);
    }
    @RestController
    class EmployeeController {
        @GetMapping("employee")
        public Mono<String> request(@RequestParam Long id , @RequestParam String name, ServerHttpRequest request){
            return Mono.just(request.getURI().toString());
        }
        @GetMapping("employee/{id}/{name}")
        public Mono<String> path(@PathVariable Long id ,@PathVariable String name, ServerHttpRequest request){
            return Mono.just(request.getURI().toString());
        }

    }
}
WebfluxConsumerApplication.java
package com.example.webfluxconsumer;

import lombok.extern.log4j.Log4j2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@Log4j2
@SpringBootApplication
public class WebfluxConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebfluxConsumerApplication.class, args);
    }

    @RestController
    class EmployeeController {
        @GetMapping("employee/{id}/{name}")
        public Mono<String> path(@PathVariable Long id, @PathVariable String name) {
            return WebClient.create(baseUrl)
                    .get()
                    .uri(uriBuilder -> uriBuilder.path("/employee2/{id}/{name}")
                            .build(id, name))
                    .retrieve()
                    .bodyToMono(String.class);
        }
        @GetMapping("employee")
        public Mono<String> request(@RequestParam Long id, @RequestParam String name) {
            return WebClient.create(baseUrl)
                    .get()
                    .uri(uriBuilder -> uriBuilder.path("/employee")
                            .queryParam("id", id)
                            .queryParam("name", name)
                            .build())
                    .retrieve()
                    .bodyToMono(String.class);
        }
    }
}

公眾號,堅持每天3分鍾視頻學習


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM