Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230928
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
javab_20230928
Commits
c336c76c
Commit
c336c76c
authored
Nov 10, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ostatnie klasy w przykładzie Jersey
parent
b3f43b8b
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
189 additions
and
0 deletions
+189
-0
build.gradle
PC39-SpringJersey/build.gradle
+1
-0
PhotoUtil.java
PC39-SpringJersey/src/main/java/sklep/photo/PhotoUtil.java
+67
-0
JerseyConfig.java
PC39-SpringJersey/src/main/java/sklep/rest/JerseyConfig.java
+2
-0
ROrder.java
PC39-SpringJersey/src/main/java/sklep/rest/ROrder.java
+41
-0
RProducts.java
PC39-SpringJersey/src/main/java/sklep/rest/RProducts.java
+78
-0
No files found.
PC39-SpringJersey/build.gradle
View file @
c336c76c
...
@@ -20,6 +20,7 @@ dependencies {
...
@@ -20,6 +20,7 @@ dependencies {
implementation
'org.springframework.boot:spring-boot-starter-jersey'
implementation
'org.springframework.boot:spring-boot-starter-jersey'
runtimeOnly
'org.postgresql:postgresql'
runtimeOnly
'org.postgresql:postgresql'
testImplementation
'org.springframework.boot:spring-boot-starter-test'
testImplementation
'org.springframework.boot:spring-boot-starter-test'
implementation
'org.glassfish.jaxb:jaxb-runtime'
}
}
tasks
.
named
(
'bootBuildImage'
)
{
tasks
.
named
(
'bootBuildImage'
)
{
...
...
PC39-SpringJersey/src/main/java/sklep/photo/PhotoUtil.java
0 → 100644
View file @
c336c76c
package
sklep
.
photo
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.server.ResponseStatusException
;
@Component
public
class
PhotoUtil
{
@Value
(
"${alx.photo_dir}"
)
private
String
photoDir
;
private
static
final
String
EXT
=
".jpg"
;
public
File
getFile
(
int
productId
)
{
Path
path
=
getPath
(
productId
);
File
file
=
path
.
toFile
();
if
(
file
.
exists
())
{
return
file
;
}
else
{
throw
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
,
"Cannot read photo for product id = "
+
productId
);
}
}
public
byte
[]
readBytes
(
int
productId
)
{
Path
path
=
getPath
(
productId
);
try
{
return
Files
.
readAllBytes
(
path
);
}
catch
(
IOException
e
)
{
// System.err.println(e);
throw
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
,
"Cannot read photo for product id = "
+
productId
);
}
}
public
void
writeStream
(
int
productId
,
InputStream
inputStream
)
{
try
{
Path
path
=
getPath
(
productId
);
Files
.
copy
(
inputStream
,
path
,
StandardCopyOption
.
REPLACE_EXISTING
);
}
catch
(
Exception
e
)
{
// wypisujemy błąd, ale metoda kończy się normalnie
e
.
printStackTrace
();
}
}
public
void
writeBytes
(
int
productId
,
byte
[]
bytes
)
{
try
{
Path
path
=
getPath
(
productId
);
Files
.
write
(
path
,
bytes
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
private
Path
getPath
(
int
productId
)
{
String
fileName
=
productId
+
EXT
;
return
Paths
.
get
(
photoDir
,
fileName
);
}
}
PC39-SpringJersey/src/main/java/sklep/rest/JerseyConfig.java
View file @
c336c76c
...
@@ -13,6 +13,8 @@ public class JerseyConfig extends ResourceConfig {
...
@@ -13,6 +13,8 @@ public class JerseyConfig extends ResourceConfig {
public
JerseyConfig
()
{
public
JerseyConfig
()
{
register
(
RHello
.
class
);
register
(
RHello
.
class
);
register
(
RTime
.
class
);
register
(
RTime
.
class
);
register
(
RProducts
.
class
);
register
(
ROrder
.
class
);
}
}
}
}
PC39-SpringJersey/src/main/java/sklep/rest/ROrder.java
0 → 100644
View file @
c336c76c
package
sklep
.
rest
;
import
java.util.List
;
import
java.util.Optional
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.server.ResponseStatusException
;
import
jakarta.ws.rs.Consumes
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.PathParam
;
import
jakarta.ws.rs.Produces
;
import
sklep.model.Order
;
import
sklep.repository.OrderRepository
;
@Path
(
"/orders"
)
@Produces
(
"application/json"
)
@Consumes
(
"application/json"
)
public
class
ROrder
{
@Autowired
private
OrderRepository
orderRepository
;
@GET
public
List
<
Order
>
readAll
()
{
return
orderRepository
.
findAll
();
}
@Path
(
"/{id}"
)
@GET
public
Order
readOne
(
@PathParam
(
"id"
)
int
id
)
{
Optional
<
Order
>
order
=
orderRepository
.
findById
(
id
);
if
(
order
.
isPresent
())
{
return
order
.
get
();
}
else
{
throw
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
,
"Nie ma zamówienia o numerze "
+
id
);
}
}
}
PC39-SpringJersey/src/main/java/sklep/rest/RProducts.java
0 → 100644
View file @
c336c76c
package
sklep
.
rest
;
import
java.net.URI
;
import
java.util.List
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.server.ResponseStatusException
;
import
jakarta.ws.rs.Consumes
;
import
jakarta.ws.rs.DELETE
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.POST
;
import
jakarta.ws.rs.PUT
;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.PathParam
;
import
jakarta.ws.rs.Produces
;
import
jakarta.ws.rs.core.Response
;
import
jakarta.ws.rs.core.UriBuilder
;
import
sklep.model.Product
;
import
sklep.photo.PhotoUtil
;
import
sklep.repository.ProductRepository
;
@Path
(
"/products"
)
public
class
RProducts
{
@Autowired
private
ProductRepository
productRepository
;
@Autowired
private
PhotoUtil
photoUtil
;
@GET
@Produces
({
"application/json"
,
"application/xml"
,
"text/plain"
})
public
List
<
Product
>
getAllProducts
()
{
return
productRepository
.
findAll
();
}
@GET
@Path
(
"/{id}"
)
@Produces
({
"application/json"
,
"application/xml"
,
"text/plain"
})
public
Product
readOne
(
@PathParam
(
"id"
)
int
productId
)
{
return
productRepository
.
findById
(
productId
)
.
orElseThrow
(()
->
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
));
}
@POST
@Consumes
({
"application/json"
,
"application/xml"
})
public
Response
saveProduct
(
Product
product
)
{
productRepository
.
save
(
product
);
URI
uri
=
UriBuilder
.
fromResource
(
RProducts
.
class
)
.
path
(
String
.
valueOf
(
product
.
getProductId
()))
.
build
();
return
Response
.
created
(
uri
).
build
();
}
@DELETE
@Path
(
"/{id}"
)
public
void
delete
(
@PathParam
(
"id"
)
int
productId
)
{
productRepository
.
deleteById
(
productId
);
}
@GET
@Path
(
"/{id}/photo"
)
@Produces
(
"image/jpeg"
)
public
byte
[]
getPhoto
(
@PathParam
(
"id"
)
int
productId
)
{
return
photoUtil
.
readBytes
(
productId
);
}
@PUT
@Path
(
"/{id}/photo"
)
@Consumes
(
"image/jpeg"
)
public
void
uploadPhoto
(
@PathParam
(
"id"
)
int
productId
,
byte
[]
bytes
)
{
photoUtil
.
writeBytes
(
productId
,
bytes
);
}
}
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