티스토리 뷰

Parameter 2개 이상 보내기

@RestController
@RequestMapping(value = "/api/v1/order")
public class DistributeController {

    // http://localhost:8080/api/v1/order/distribute?id=1111&name=krk 이렇게 요청한다.
    @GetMapping(value = "/distribute")
    public String getOrder(@RequestParam int id, @RequestParam String name) {
        System.out.println(id);
        System.out.println(name);
}

결과

1111
krk

TransferController.java

@Slf4j
@RestController
@RequestMapping("/api/v1")
public class TransferController {

    private TransferService transferService;

    public TransferController(TransferService transferService) {
        this.transferService = transferService;
    }

    @GetMapping("/transfer")
    Mono<ServerResponse> getTransfer(ServerRequest req) {
        return ok().body(fromObject(req.pathVariable("name")));
    }

    @GetMapping("/transferp")
    Mono<String> getTransferp() {
        return Mono.just("hello");
    }

    @GetMapping("/transfer2/{name}")
    Mono<String> getTransfer2(@PathVariable String name) {
        return Mono.just("hello" + name);
    }

    @GetMapping("/transfer3")
    Mono<String> getTransfer3(Transfer transfer) {
        return Mono.just("hello" + transfer.getDirection());
    }

    // Mono로 받으면 좋은점 .map등을 쓸 수 있음
    @GetMapping("/transfer4")
    Mono<String> getTransfer4(@RequestBody Mono<Transfer> transfer) {
        return transfer.map(t -> "hello" + t.getDirection());
    }

    @PostMapping(value = "/transfer5")
    Mono<String> getTransfer5(@RequestBody Flux<Transfer> transfer) {
        return Mono.just("hello4");
    }

    @PostMapping(value = "/transfer6")
    Mono<String> getTransfer6(@RequestBody Transfer transfer) {
        transferService.saveToDb(transfer);
        log.info("=>{}", transfer);
        return Mono.just("hello-withdraw");
    }
}

TransferControllerTest

@RunWith(SpringRunner.class)
@WebFluxTest
public class TransferControllerTest {

    @Autowired
    private WebTestClient webTestClient;

    @Test
    public void getWithParam() {
        webTestClient.get().uri("/api/v2/transfer2/{name}", "2")
                .exchange()
                .expectStatus().isOk()
                .expectBody(String.class)
                .isEqualTo("hello" + "2");
    }

    @Test
    public void get() {
        webTestClient.post().uri("/api/v2/transfer3")
                .exchange()
                .expectStatus().isOk()
                .expectBody(String.class)
                .isEqualTo("hello");
    }


    @Test
    public void post() {
        webTestClient.post().uri("/api/v2/transfer5")
                .exchange()
                .expectStatus().isOk()
                .expectBody(String.class)
                .isEqualTo("hello" + "2");
    }

    @Test
    public void postWithParam() {
        webTestClient.post().uri("/api/v2/transfer6")
                .body(BodyInserters.fromObject(new Transfer(LocalDateTime.now(), BigDecimal.TEN, "aa", "withdraw")))
                .exchange()
                .expectStatus().isOk()
                .expectBody(String.class)
                .isEqualTo("hello-" + "withdraw");
    }
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함