Commit b3f43b8b by Patryk Czarnik

Jersey: proste przykłady bez bazy danych

parent c2d729fa
package sklep.rest;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JerseyConfig extends ResourceConfig {
/* Musimy zapewnić, aby podczas startu aplikacji na obiekcie ResourceConfig (z Jerseya)
* została wywołan metoda register dla wszystkich "resource classes", które wchodzą w skład aplikacji JAX-RS.
* W wersji Springowej nie ma klasy typu Application.
*/
public JerseyConfig() {
register(RHello.class);
register(RTime.class);
}
}
package sklep.rest;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
@Path("/hello")
public class RHello {
@GET
public String hello() {
return "Hello <b>REST</b>";
}
}
package sklep.rest;
import java.time.LocalDateTime;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
@Path("/dt")
public class RTime {
// Obiekt tej klasy jest tworzony za każdym razem do obsługi każdego pojedynczego zapytania.
private LocalDateTime dt = LocalDateTime.now();
{ System.out.println("Jest tworzony obiekt RTime , dt = " + dt); }
// działa pod adresem: /dt
@GET
public String getDateTime() {
return dt.toString();
}
// działa pod adresem: /dt/date
@GET
@Path("/date")
public String getDate() {
return dt.toLocalDate().toString();
}
@GET
@Path("/time")
public String getTime() {
return dt.toLocalTime().toString();
}
@GET
@Path("/time/second")
public int 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