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()
์ ํธ์ถํ ๊ฒ์ด๋ค.