Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20240528-BJava
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
20240528-BJava
Commits
b36e336f
Commit
b36e336f
authored
Jun 28, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Proste przykłady REST bez modelu
parent
2d84654e
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
138 additions
and
0 deletions
+138
-0
DataCzas.java
PC34-SpringJersey/src/main/java/sklep/rest/DataCzas.java
+72
-0
Hello.java
PC34-SpringJersey/src/main/java/sklep/rest/Hello.java
+13
-0
JerseyConfig.java
PC34-SpringJersey/src/main/java/sklep/rest/JerseyConfig.java
+19
-0
Kalkulator.java
PC34-SpringJersey/src/main/java/sklep/rest/Kalkulator.java
+34
-0
No files found.
PC34-SpringJersey/src/main/java/sklep/rest/DataCzas.java
0 → 100644
View file @
b36e336f
package
sklep
.
rest
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.Produces
;
import
java.time.LocalDate
;
import
java.time.LocalDateTime
;
import
java.time.LocalTime
;
@Path
(
"/now"
)
// @Singleton
public
class
DataCzas
{
// 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
now
=
LocalDateTime
.
now
();
{
// ten blok wykona się podczas tworzenia każdego obiektu
System
.
out
.
println
(
"Powstał obiekt DataCzas z czasem równym "
+
now
);
}
// ta metoda obsługuje adres .../api/now
@GET
public
LocalDateTime
odczytajDataICzas
()
{
return
now
;
}
// ta metoda obsługuje adres .../api/now/date
@GET
@Path
(
"/date"
)
public
LocalDate
odczytajDate
()
{
return
now
.
toLocalDate
();
}
// ta metoda obsługuje adres .../api/now/date/year
@GET
@Path
(
"/date/year"
)
public
int
odczytajRok
()
{
return
now
.
getYear
();
}
// ta metoda obsługuje adres .../api/now/date/month
@GET
@Path
(
"/date/month"
)
public
int
odczytajMiesiac
()
{
return
now
.
getMonthValue
();
}
// ta metoda obsługuje adres .../api/now/date/day
@GET
@Path
(
"/date/day"
)
public
int
odczytajDzien
()
{
return
now
.
getDayOfMonth
();
}
// ta metoda obsługuje adres .../api/now/time
@GET
@Path
(
"/time"
)
public
LocalTime
odczytajCzas
()
{
return
now
.
toLocalTime
();
}
// ta metoda obsługuje adres .../api/now/time/second
@GET
@Path
(
"/time/second"
)
public
int
odczytajSekunde
()
{
return
now
.
getSecond
();
}
}
PC34-SpringJersey/src/main/java/sklep/rest/Hello.java
0 → 100644
View file @
b36e336f
package
sklep
.
rest
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.Path
;
@Path
(
"/hello"
)
public
class
Hello
{
@GET
public
String
sayHello
()
{
return
"Hello World!"
;
}
}
PC34-SpringJersey/src/main/java/sklep/rest/JerseyConfig.java
0 → 100644
View file @
b36e336f
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
(
Hello
.
class
);
register
(
DataCzas
.
class
);
register
(
Kalkulator
.
class
);
}
}
PC34-SpringJersey/src/main/java/sklep/rest/Kalkulator.java
0 → 100644
View file @
b36e336f
package
sklep
.
rest
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.PathParam
;
@Path
(
"/calc"
)
public
class
Kalkulator
{
@GET
@Path
(
"/{x}+{y}"
)
public
long
dodaj
(
@PathParam
(
"x"
)
long
a
,
@PathParam
(
"y"
)
long
b
)
{
return
a
+
b
;
}
@GET
@Path
(
"/{x}-{y}"
)
public
long
odejmij
(
@PathParam
(
"x"
)
long
a
,
@PathParam
(
"y"
)
long
b
)
{
return
a
-
b
;
}
@GET
@Path
(
"/{x}*{y}"
)
public
long
pomnoz
(
@PathParam
(
"x"
)
long
a
,
@PathParam
(
"y"
)
long
b
)
{
return
a
*
b
;
}
@GET
@Path
(
"/{x}/{y}"
)
public
long
podziel
(
@PathParam
(
"x"
)
long
a
,
@PathParam
(
"y"
)
long
b
)
{
return
a
/
b
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment