WebFlux系列(八)WebClient Uri传参

#Java#Spring#WebFlux#WebClient#Post#Get#传参#Uri#

WebClient如何通过Uri以Get方式传参

视频讲解 : https://www.bilibili.com/video/av82953521/

WebFlux系列(八)WebClient Uri传参_第1张图片

 

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 request(@RequestParam Long id , @RequestParam String name, ServerHttpRequest request){
            return Mono.just(request.getURI().toString());
        }
        @GetMapping("employee/{id}/{name}")
        public Mono 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 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 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分钟视频学习

WebFlux系列(八)WebClient Uri传参_第2张图片

你可能感兴趣的:(WebFlux系列(八)WebClient Uri传参)