Created
February 23, 2026 13:14
-
-
Save MichalBrylka/c05d001de4d7bd279024d8d50ab7f955 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.time.LocalDate; | |
| import java.util.UUID; | |
| public class Booking { | |
| public String id; | |
| public String guestName; | |
| public String roomNumber; | |
| public LocalDate checkIn; | |
| public LocalDate checkOut; | |
| public Booking() { | |
| // Jackson requires default constructor | |
| } | |
| public Booking(String guestName, String roomNumber, | |
| LocalDate checkIn, LocalDate checkOut) { | |
| this.id = UUID.randomUUID().toString(); | |
| this.guestName = guestName; | |
| this.roomNumber = roomNumber; | |
| this.checkIn = checkIn; | |
| this.checkOut = checkOut; | |
| } | |
| } | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | |
| import jakarta.servlet.http.HttpServlet; | |
| import jakarta.servlet.http.HttpServletRequest; | |
| import jakarta.servlet.http.HttpServletResponse; | |
| import java.io.IOException; | |
| import java.time.LocalDate; | |
| import java.util.*; | |
| import java.util.concurrent.ConcurrentHashMap; | |
| public class BookingServlet extends HttpServlet { | |
| private final Map<String, Booking> bookings = new ConcurrentHashMap<>(); | |
| private final ObjectMapper mapper = new ObjectMapper() | |
| .registerModule(new JavaTimeModule()); | |
| @Override | |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) | |
| throws IOException { | |
| resp.setContentType("application/json"); | |
| String path = req.getPathInfo(); | |
| if (path == null || path.equals("/")) { | |
| // GET /bookings | |
| mapper.writeValue(resp.getOutputStream(), bookings.values()); | |
| return; | |
| } | |
| // GET /bookings/{id} | |
| String id = path.substring(1); | |
| Booking booking = bookings.get(id); | |
| if (booking == null) { | |
| resp.setStatus(HttpServletResponse.SC_NOT_FOUND); | |
| return; | |
| } | |
| mapper.writeValue(resp.getOutputStream(), booking); | |
| } | |
| @Override | |
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) | |
| throws IOException { | |
| Booking input = mapper.readValue(req.getInputStream(), Booking.class); | |
| // basic validation | |
| if (input.guestName == null || input.roomNumber == null || | |
| input.checkIn == null || input.checkOut == null) { | |
| resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); | |
| return; | |
| } | |
| Booking booking = new Booking( | |
| input.guestName, | |
| input.roomNumber, | |
| input.checkIn, | |
| input.checkOut | |
| ); | |
| bookings.put(booking.id, booking); | |
| resp.setStatus(HttpServletResponse.SC_CREATED); | |
| resp.setContentType("application/json"); | |
| mapper.writeValue(resp.getOutputStream(), booking); | |
| } | |
| @Override | |
| protected void doDelete(HttpServletRequest req, HttpServletResponse resp) | |
| throws IOException { | |
| String path = req.getPathInfo(); | |
| if (path == null || path.equals("/")) { | |
| resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); | |
| return; | |
| } | |
| String id = path.substring(1); | |
| Booking removed = bookings.remove(id); | |
| if (removed == null) { | |
| resp.setStatus(HttpServletResponse.SC_NOT_FOUND); | |
| return; | |
| } | |
| resp.setStatus(HttpServletResponse.SC_NO_CONTENT); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.eclipse.jetty.server.Server; | |
| import org.eclipse.jetty.servlet.ServletContextHandler; | |
| import org.eclipse.jetty.servlet.ServletHolder; | |
| public class Main { | |
| public static void main(String[] args) throws Exception { | |
| Server server = new Server(8080); | |
| ServletContextHandler context = new ServletContextHandler(); | |
| context.setContextPath("/"); | |
| context.addServlet(new ServletHolder(new BookingServlet()), "/bookings/*"); | |
| server.setHandler(context); | |
| server.start(); | |
| server.join(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <dependencies> | |
| <!-- Jetty --> | |
| <dependency> | |
| <groupId>org.eclipse.jetty</groupId> | |
| <artifactId>jetty-server</artifactId> | |
| <version>11.0.17</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.eclipse.jetty</groupId> | |
| <artifactId>jetty-servlet</artifactId> | |
| <version>11.0.17</version> | |
| </dependency> | |
| <!-- Jackson --> | |
| <dependency> | |
| <groupId>com.fasterxml.jackson.core</groupId> | |
| <artifactId>jackson-databind</artifactId> | |
| <version>2.17.0</version> | |
| </dependency> | |
| </dependencies> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment