반응형

테스트 API

@Slf4j
@RestController
public class ExampleController {
    @Data
    public static class Image {
        private String name;
        private FilePart imageFile;
    }

    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public static class Student {
        private String name;
        private int age;
    }

    @GetMapping("/api/students/one")
    public Mono<Student> getStudentOne() {
        return Mono.just(new Student("tyler", 30));
    }

    @GetMapping("/api/students")
    public Flux<Student> getStudents() {
        return Flux.just(
            new Student("tyler", 30),
            new Student("tom", 31),
            new Student("jane", 32)
        );
    }

    @PostMapping("/api/students")
    public Mono<Student> addStudent(@RequestBody Student student) {
        log.info("add : {}", student);
        return Mono.just(student);
    }

    @PostMapping("/api/images")
    public Mono<String> uploadImage(Image image) {
        return image.getImageFile()
            .transferTo(new File(image.getImageFile().filename()))
            .thenReturn(image.getName());
    }
}

Create WebClient

Mono<Student> responseMono = WebClient.create("http://localhost:8080")
    .get()
    .uri("/api/students/one")
    .retrieve()
    .bodyToMono(Student.class);

StepVerifier.create(responseMono.log())
    .expectNext(new Student("tyler", 30))
    .verifyComplete();

Create WebClient 2

Mono<Student> responseMono = WebClient.create()
    .get()
    .uri("http://localhost:8080/api/students/one")
    .retrieve()
    .bodyToMono(Student.class);

StepVerifier.create(responseMono.log())
    .expectNext(new Student("tyler", 30))
    .verifyComplete();

Build WebClient

WebClient webClient = WebClient.builder()
    .baseUrl("http://localhost:8080")
    .build();

Mono<Student> responseMono = webClient
    .get()
    .uri("/api/students/one")
    .retrieve()
    .bodyToMono(Student.class);

StepVerifier.create(responseMono.log())
    .expectNext(new Student("tyler", 30))
    .verifyComplete();

Build WebClient 2

WebClient webClient = WebClient.builder().build();

Mono<Student> responseMono = webClient
    .get()
    .uri("http://localhost:8080/api/students/one")
    .retrieve()
    .bodyToMono(Student.class);

StepVerifier.create(responseMono.log())
    .expectNext(new Student("tyler", 30))
    .verifyComplete();

Status Handling

WebClient webClient = WebClient.builder().build();

Mono<Student> responseMono = webClient
    .get()
    .uri("http://localhost:8080/api/students/one")
    .exchangeToMono(response -> {
        if (response.statusCode().is2xxSuccessful()) {
            return response.bodyToMono(Student.class);
        }

        return Mono.error(new IllegalStateException());
    });

StepVerifier.create(responseMono.log())
    .expectNext(new Student("tyler", 30))
    .verifyComplete();

List Response Body

WebClient webClient = WebClient.builder().build();

Flux<Student> responseMono = webClient
    .get()
    .uri("http://localhost:8080/api/students")
    .retrieve()
    .bodyToFlux(Student.class);

StepVerifier.create(responseMono.log())
    .expectNext(new Student("tyler", 30))
    .expectNext(new Student("tom", 31))
    .expectNext(new Student("jane", 32))
    .verifyComplete();

Request Body

WebClient webClient = WebClient.builder().build();

Mono<Student> responseMono = webClient
    .post()
    .uri("http://localhost:8080/api/students")
    .contentType(MediaType.APPLICATION_JSON)
    .accept(MediaType.APPLICATION_JSON)
    .bodyValue(new Student("brian", 22))
    .retrieve()
    .bodyToMono(Student.class);

StepVerifier.create(responseMono.log())
    .expectNextMatches((response) -> "brian".equals(response.getName()) && 22 == response.getAge())
    .verifyComplete();

Request Body 2

WebClient webClient = WebClient.builder().build();

Mono<Student> responseMono = webClient
    .post()
    .uri("http://localhost:8080/api/students")
    .contentType(MediaType.APPLICATION_JSON)
    .accept(MediaType.APPLICATION_JSON)
    .body(BodyInserters.fromPublisher(
        Mono.just(new Student("brian", 22)),
        Student.class
    ))
    .retrieve()
    .bodyToMono(Student.class);

StepVerifier.create(responseMono.log())
    .expectNextMatches((response) -> "brian".equals(response.getName()) && 22 == response.getAge())
    .verifyComplete();

Request Multipart

WebClient webClient = WebClient.builder().build();

Mono<String> responseMono = webClient
    .post()
    .uri("http://localhost:8080/api/images")
    .body(BodyInserters
        .fromMultipartData("name", "john")
        .with("imageFile", new FileSystemResource(new File("test_image.png")))
    )
    .retrieve()
    .bodyToMono(String.class);

StepVerifier.create(responseMono.log())
    .expectNext("john")
    .verifyComplete();
반응형

'Development > WebFlux' 카테고리의 다른 글

[WebFlux] Spring Data Reactive  (0) 2021.07.17
[WebFlux] Stream API  (0) 2021.07.17
[WebFlux] Test  (0) 2021.07.17
[WebFlux] Controller  (0) 2021.07.17
[WebFlux] Introduction  (0) 2021.07.13

+ Recent posts