Commit 0f34b3fc by Patryk Czarnik

TimeResource - dt jako pole i wyjaśnienie polityki "per request"

parent 7157b557
...@@ -10,40 +10,63 @@ import jakarta.ws.rs.Produces; ...@@ -10,40 +10,63 @@ import jakarta.ws.rs.Produces;
@Path("/dt") @Path("/dt")
@Produces("text/plain;charset=UTF-8") @Produces("text/plain;charset=UTF-8")
// @Singleton
public class TimeResource { public class TimeResource {
// W JAX-RS domyślne zachowanie jest takie, że dla każdego requestu tworzony jest nowy obiekt klasy obsługującej zapytania.
// (polityka "per request"; można ją zmienić za pomocą adnotacji @Singleton - wtedy jeden obiekt obsłuży wszystkie zapytania)
// Daje to możliwość zapisania pewnych ulotnych informacji w polach prywatnych tej klasy - nikt nam ich nie nadpisze.
private LocalDateTime dt = LocalDateTime.now();
{
System.out.println("Powstaje obiekt TimeResource z odczytanym czasem " + dt);
}
// ta metoda obsługuje adres .../dt // ta metoda obsługuje adres .../dt
@GET @GET
public LocalDateTime odczytajDataICzas() { public LocalDateTime odczytajDataICzas() {
return LocalDateTime.now(); return dt;
} }
// ta metoda obsługuje adres .../dt/date // ta metoda obsługuje adres .../dt/date
@GET @GET
@Path("/date") @Path("/date")
public LocalDate odczytajDate() { public LocalDate odczytajDate() {
return LocalDate.now(); return dt.toLocalDate();
} }
// ta metoda obsługuje adres .../dt/date/year // ta metoda obsługuje adres .../dt/date/year
@GET @GET
@Path("/date/year") @Path("/date/year")
public int odczytajRok() { public int odczytajRok() {
return odczytajDate().getYear(); return dt.getYear();
}
// ta metoda obsługuje adres .../dt/date/month
@GET
@Path("/date/month")
public int odczytajMiesiac() {
return dt.getMonthValue();
}
// ta metoda obsługuje adres .../dt/date/day
@GET
@Path("/date/day")
public int odczytajDzien() {
return dt.getDayOfMonth();
} }
// ta metoda obsługuje adres .../dt/time // ta metoda obsługuje adres .../dt/time
@GET @GET
@Path("/time") @Path("/time")
public LocalTime odczytajCzas() { public LocalTime odczytajCzas() {
return LocalTime.now(); return dt.toLocalTime();
} }
// ta metoda obsługuje adres .../dt/time/second // ta metoda obsługuje adres .../dt/time/second
@GET @GET
@Path("/time/second") @Path("/time/second")
public int odczytajSekunde() { public int odczytajSekunde() {
return odczytajCzas().getSecond(); return dt.getSecond();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment