• WebFlux系列(五)WebClient基本应用


    #Java#Spring#WebFlux#WebClient#Reactor#

    WebClient如何调用远程接口

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

    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.MediaType;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import reactor.core.publisher.Flux;
    
    import java.time.Duration;
    
    @Log4j2
    @SpringBootApplication
    public class WebfluxServerApplication extends BaseApplication {
        public static void main(String[] args) {
            SpringApplication.run(WebfluxServerApplication.class, args);
        }
    
        @RestController
        class EmployeeController {
            @GetMapping(value = "employees",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
            public Flux<Employee> findAll(){
                return Flux.range(1,5).map(val ->{
                    return list.stream().filter(employee -> employee.getId() ==val).findFirst().get();
                }).delayElements(Duration.ofMillis(1000));
            }
        }
    }
    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.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    import org.springframework.web.reactive.function.client.ClientResponse;
    import org.springframework.web.reactive.function.client.WebClient;
    import reactor.core.publisher.Flux;
    import reactor.core.publisher.Mono;
    
    import java.util.Arrays;
    import java.util.List;
    
    @Log4j2
    @SpringBootApplication
    public class WebfluxConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(WebfluxConsumerApplication.class, args);
        }
    
        @RestController
        class EmployeeController {
            @GetMapping(value = "employees", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
            public Flux<Employee> findAll() {
                return WebClient.create("localhost:8080/employees").get().retrieve().bodyToFlux(Employee.class);
            }
        }
    }

    公众号,坚持每天3分钟视频学习

  • 相关阅读:
    2020-2021-01 20201230 《信息安全专业导论》第十一周学习总结
    2020-2021-01 20201230 《信息安全专业导论》第十周学习总结
    四则运算
    编程语言
    2020-2021-01 20201230 《信息安全专业导论》第九周学习总结
    2020-2021-01 20201230 《信息安全专业导论》第八周学习总结
    编程之路
    2020-2021-01 20201230 《信息安全专业导论》第七周学习总结
    进制转化
    密码算法及理论总结
  • 原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/12165924.html
Copyright © 2020-2023  润新知