WebFlux系列(八)WebClient Uri傳參

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

WebClient如何經過Uri以Get方式傳參java

視頻講解 : https://www.bilibili.com/vide...react


WebfluxServerApplication.javaweb

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.javaspring

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分鐘視頻學習
app

相關文章
相關標籤/搜索