Bibi's DevLog ๐Ÿค“๐ŸŽ

[Spring] ๊ฐ™์€ URL์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ๋„ ๋‹ค๋ฅธ ํ•œ URL์— ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์„ ๋ฐ›๊ณ  ์‹ถ์„ ๋•Œ ๋งตํ•‘ํ•˜๋Š” ๋ฒ• ๋ณธ๋ฌธ

๐Ÿ–ฅ BE ๋ฐฑ์—”๋“œ/Spring ์Šคํ”„๋ง

[Spring] ๊ฐ™์€ URL์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ๋„ ๋‹ค๋ฅธ ํ•œ URL์— ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์„ ๋ฐ›๊ณ  ์‹ถ์„ ๋•Œ ๋งตํ•‘ํ•˜๋Š” ๋ฒ•

๋น„๋น„ bibi 2021. 5. 28. 22:18

[Spring] ๊ฐ™์€ URL์— ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์„ ๋•Œ ๋งตํ•‘ํ•˜๋Š” ๋ฒ•

๊ฐ™์€ URL์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ๋„, ๋‹ค๋ฅธ ํ•œ URL์€ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์œผ๋กœ ๊ฐ’์„ ๋ฐ›์•„์˜ค๊ณ  ์‹ถ์„ ๋•Œ

@GetMapping(params = {"checkIn", "checkOut", "minPrice", "maxPrice", "numberOfPeople"})

์™€ ๊ฐ™์ด ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์œผ๋กœ ๋ฐ›์•„์˜ค๊ณ ์ž ํ•˜๋Š” ๊ฐ’์˜ ํ‚ค๋ฅผ ๋‚˜์—ดํ•ด @GetMapping์˜ params = {...}์˜ต์…˜์œผ๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด ๋งตํ•‘ํ•œ๋‹ค.

@RestController
@RequestMapping("/api/rooms")
public class RoomController {

    private RoomRepository roomRepository;
    private WishRepository wishRepository;

    public RoomController(RoomRepository roomRepository, WishRepository wishRepository) {
        this.roomRepository = roomRepository;
        this.wishRepository = wishRepository;
    }

    @GetMapping
    public Rooms showRooms() {
        List<RoomDTO> roomDTOS = roomRepository.findAll().stream()
                .map(RoomDTO::toRoomDTO)
                .collect(Collectors.toList());
        return new Rooms(roomDTOS);
    }

    @GetMapping(params = {"checkIn", "checkOut", "minPrice", "maxPrice", "numberOfPeople"})
    public Rooms searchRooms(@RequestParam("checkIn") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate checkIn,
                             @RequestParam("checkOut") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate checkOut,
                             @RequestParam("minPrice") int minPrice, @RequestParam("maxPrice") int maxPrice,
                             @RequestParam("numberOfPeople") int numberOfPeople) {
        List<RoomDTO> roomDTOS = roomRepository.getFilteredRooms(checkIn, checkOut, minPrice, maxPrice, numberOfPeople).stream()
                .map(RoomDTO::toRoomDTO)
                .collect(Collectors.toList());
        return new Rooms(roomDTOS);
    }

http://localhost:8080/api/rooms์œผ๋กœ GET์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด showRooms()์„ ํ˜ธ์ถœํ•˜๊ณ ,

http://localhost:8080/api/rooms?checkIn=2021.05.05&checkOut=2021.05.06&minPrice=50000&maxPrice=60000&numberOfPeople=2์™€ ๊ฐ™์ด GET์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด searchRooms()์„ ํ˜ธ์ถœํ•  ๊ฒƒ์ด๋‹ค.