Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20230403
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
20230403
Commits
8a732f16
Commit
8a732f16
authored
May 26, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Jersey - różne proste przykłady
parent
90895118
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
295 additions
and
0 deletions
+295
-0
JerseyConfig.java
PC37-Jersey/src/main/java/com/demo/JerseyConfig.java
+4
-0
Kalkulator.java
PC37-Jersey/src/main/java/com/demo/Kalkulator.java
+34
-0
Kontekst.java
PC37-Jersey/src/main/java/com/demo/Kontekst.java
+65
-0
Parametry.java
PC37-Jersey/src/main/java/com/demo/Parametry.java
+102
-0
Time.java
PC37-Jersey/src/main/java/com/demo/Time.java
+85
-0
application.properties
PC37-Jersey/src/main/resources/application.properties
+5
-0
No files found.
PC37-Jersey/src/main/java/com/demo/JerseyConfig.java
View file @
8a732f16
...
...
@@ -8,6 +8,10 @@ public class JerseyConfig extends ResourceConfig {
public
JerseyConfig
()
{
register
(
Hello
.
class
);
register
(
Kontekst
.
class
);
register
(
Parametry
.
class
);
register
(
Kalkulator
.
class
);
register
(
Time
.
class
);
}
}
PC37-Jersey/src/main/java/com/demo/Kalkulator.java
0 → 100644
View file @
8a732f16
package
com
.
demo
;
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
;
}
}
PC37-Jersey/src/main/java/com/demo/Kontekst.java
0 → 100644
View file @
8a732f16
package
com
.
demo
;
import
java.util.Arrays
;
import
java.util.Map
;
import
jakarta.servlet.ServletConfig
;
import
jakarta.servlet.ServletContext
;
import
jakarta.servlet.http.HttpServletRequest
;
import
jakarta.servlet.http.HttpServletResponse
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.Produces
;
import
jakarta.ws.rs.core.Context
;
import
jakarta.ws.rs.core.HttpHeaders
;
import
jakarta.ws.rs.core.Request
;
import
jakarta.ws.rs.core.SecurityContext
;
import
jakarta.ws.rs.core.UriInfo
;
@Path
(
"/kontekst"
)
@Produces
(
"text/plain"
)
public
class
Kontekst
{
@GET
public
String
info
(
@Context
UriInfo
uriInfo
,
@Context
SecurityContext
securityContext
,
@Context
Request
restRequest
,
@Context
HttpHeaders
headers
,
@Context
ServletContext
servletContext
,
@Context
ServletConfig
servletConfig
,
@Context
HttpServletRequest
servletRequest
,
@Context
HttpServletResponse
servletResponse
)
{
StringBuilder
b
=
new
StringBuilder
();
b
.
append
(
"UriInfo\n"
);
b
.
append
(
"Base URI : "
).
append
(
uriInfo
.
getBaseUri
()).
append
(
'\n'
);
b
.
append
(
"Absolute path: "
).
append
(
uriInfo
.
getAbsolutePath
()).
append
(
'\n'
);
b
.
append
(
"Path : "
).
append
(
uriInfo
.
getPath
()).
append
(
'\n'
);
b
.
append
(
"\nHttpHeaders\n"
);
b
.
append
(
headers
.
getRequestHeaders
());
// Request restRequest - wiąże się z obsługą keszowania i typów zawartości
b
.
append
(
"\nServletContext\n"
);
b
.
append
(
"ServlerInfo: "
).
append
(
servletContext
.
getServerInfo
()).
append
(
'\n'
);
b
.
append
(
"\nServletRequest\n"
);
b
.
append
(
"RemoteAddr: "
).
append
(
servletRequest
.
getRemoteAddr
()).
append
(
'\n'
);
b
.
append
(
"LocalAddr : "
).
append
(
servletRequest
.
getLocalAddr
()).
append
(
'\n'
);
b
.
append
(
"URI : "
).
append
(
servletRequest
.
getRequestURI
()).
append
(
'\n'
);
b
.
append
(
"Parametry :\n"
);
for
(
Map
.
Entry
e
:
servletRequest
.
getParameterMap
().
entrySet
())
{
String
[]
v
=
(
String
[])
e
.
getValue
();
b
.
append
(
" * "
+
e
.
getKey
()
+
" : "
+
Arrays
.
toString
(
v
)
+
"\n"
);
}
b
.
append
(
"\nZalogowany wg serwletu: "
).
append
(
servletRequest
.
getRemoteUser
()).
append
(
'\n'
);
b
.
append
(
"Zalogowany wg SecurityContext: "
).
append
(
securityContext
.
getUserPrincipal
()).
append
(
'\n'
);
return
b
.
toString
();
}
}
PC37-Jersey/src/main/java/com/demo/Parametry.java
0 → 100644
View file @
8a732f16
package
com
.
demo
;
import
java.time.LocalDateTime
;
import
java.util.Arrays
;
import
jakarta.ws.rs.CookieParam
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.HeaderParam
;
import
jakarta.ws.rs.MatrixParam
;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.PathParam
;
import
jakarta.ws.rs.Produces
;
import
jakarta.ws.rs.QueryParam
;
import
jakarta.ws.rs.core.NewCookie
;
import
jakarta.ws.rs.core.Response
;
/* Przykładowe adresy z parametrami:
/rest1/parametry/query?a=Ala&b=Ola&b=Ela&t=Basia&t=Kasia&t=Zosia
/rest1/parametry/matrix;a=Ala;b=Ola;b=Ela;t=Basia;t=Kasia;t=Zosia
*/
@Path
(
"/parametry"
)
@Produces
(
"text/plain"
)
public
class
Parametry
{
@GET
@Path
(
"/query"
)
public
String
query
(
@QueryParam
(
"a"
)
String
a
,
@QueryParam
(
"b"
)
String
b
,
@QueryParam
(
"t"
)
String
[]
t
)
{
return
"Parametr a = "
+
a
+
"\nParametr b = "
+
b
+
"\nTablica: "
+
Arrays
.
toString
(
t
);
}
@GET
@Path
(
"/matrix"
)
public
String
matrix
(
@MatrixParam
(
"a"
)
String
a
,
@MatrixParam
(
"b"
)
String
b
,
@MatrixParam
(
"t"
)
String
[]
t
)
{
return
"Parametr a = "
+
a
+
"\nParametr b = "
+
b
+
"\nTablica: "
+
Arrays
.
toString
(
t
);
}
// /rest1/parametry/path/Ala/123/98765qwerty@res-zta
@GET
@Path
(
"/path/{a}/{b}/{cyfry:\\d+}{litery:\\w+}{reszta}"
)
public
String
pathParam
(
@PathParam
(
"a"
)
String
a
,
@PathParam
(
"b"
)
String
b
,
@PathParam
(
"cyfry"
)
String
cyfry
,
@PathParam
(
"litery"
)
String
litery
,
@PathParam
(
"reszta"
)
String
reszta
)
{
return
"Parametr a = "
+
a
+
"\nParametr b = "
+
b
+
"\nCyfry: "
+
cyfry
+
"\nLitery: "
+
litery
+
"\nReszta: "
+
reszta
;
}
@GET
@Path
(
"/headers"
)
public
String
headers
(
@HeaderParam
(
"accept"
)
String
accept
,
@HeaderParam
(
"user-agent"
)
String
agent
)
{
return
"Accept: "
+
accept
+
"\nUser-Agent: "
+
agent
;
}
@GET
@Path
(
"/cookies"
)
public
String
cookies
(
@CookieParam
(
"ciacho"
)
String
ciacho
,
@CookieParam
(
"JSESSIONID"
)
String
sessionId
)
{
return
"Ciacho: "
+
ciacho
+
"\nSesja: "
+
sessionId
;
}
@GET
@Path
(
"/ustaw"
)
// ustawia ciacho
public
Response
ustawCiacho
()
{
String
ciacho
=
LocalDateTime
.
now
().
toString
();
return
Response
.
ok
()
.
cookie
(
new
NewCookie
(
"ciacho"
,
ciacho
))
.
type
(
"text/plain"
)
.
entity
(
"Ustawiam ciacho na: "
+
ciacho
)
.
build
();
}
}
PC37-Jersey/src/main/java/com/demo/Time.java
0 → 100644
View file @
8a732f16
package
com
.
demo
;
import
java.time.LocalDate
;
import
java.time.LocalDateTime
;
import
java.time.LocalTime
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.Produces
;
@Path
(
"/dt"
)
@Produces
(
"text/plain"
)
public
class
Time
{
private
LocalDateTime
dt
=
LocalDateTime
.
now
();
{
System
.
out
.
println
(
"Jest tworzony obiekt klasy DataICzas, dt = "
+
dt
);
}
@GET
public
LocalDateTime
dt
()
{
return
dt
;
}
@Path
(
"/date"
)
@GET
public
LocalDate
data
()
{
return
dt
.
toLocalDate
();
}
@Path
(
"/date/year"
)
@GET
public
int
year
()
{
return
dt
.
getYear
();
}
@Path
(
"/date/month"
)
@GET
public
int
month
()
{
return
dt
.
getMonthValue
();
}
@Path
(
"/date/day"
)
@GET
public
int
day
()
{
return
dt
.
getDayOfMonth
();
}
@Path
(
"/date/wd"
)
@GET
public
String
weekday
()
{
return
dt
.
getDayOfWeek
().
toString
();
}
@Path
(
"/date/doy"
)
@GET
public
int
doy
()
{
return
dt
.
getDayOfYear
();
}
@Path
(
"/time"
)
@GET
public
LocalTime
time
()
{
return
dt
.
toLocalTime
();
}
@Path
(
"/time/hour"
)
@GET
public
int
hour
()
{
return
dt
.
getHour
();
}
@Path
(
"/time/minute"
)
@GET
public
int
minute
()
{
return
dt
.
getMinute
();
}
@Path
(
"/time/second"
)
@GET
public
int
second
()
{
return
dt
.
getSecond
();
}
}
PC37-Jersey/src/main/resources/application.properties
0 → 100644
View file @
8a732f16
spring.datasource.url
=
jdbc:postgresql://localhost:5432/sklep
spring.datasource.username
=
kurs
spring.datasource.password
=
abc123
alx.photo_dir
=
/home/patryk/sklep/foto
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