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
36dd92e3
Commit
36dd92e3
authored
Jun 14, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Response i ExceptionMapper
parent
f39b5379
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
2 deletions
+42
-2
RProductsHtml.java
PC27-RestSerwer/src/main/java/sklep/rest/RProductsHtml.java
+15
-2
RecordNotFoundMapper.java
...er/src/main/java/sklep/rest/ext/RecordNotFoundMapper.java
+27
-0
No files found.
PC27-RestSerwer/src/main/java/sklep/rest/RProductsHtml.java
View file @
36dd92e3
package
sklep
.
rest
;
import
jakarta.ws.rs.*
;
import
jakarta.ws.rs.core.Response
;
import
sklep.db.DBConnection
;
import
sklep.db.DBException
;
import
sklep.db.ProductDAO
;
...
...
@@ -27,13 +28,25 @@ public class RProductsHtml {
}
}
// W tej metodzie pokazuję, jak wyglądaloby zwracanie różnych kodów odpowiedzi w zależności od sytuacji.
// Używamy klasy Response
@GET
@Path
(
"/{id}"
)
public
String
oneProduct
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
public
Response
oneProduct
(
@PathParam
(
"id"
)
int
productId
)
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
Product
product
=
productDAO
.
findById
(
productId
);
return
"<!DOCTYPE html>\n<html><body>"
+
product
.
toHtml
()
+
"</body></html>"
;
return
Response
.
ok
()
.
entity
(
"<!DOCTYPE html>\n<html><body>"
+
product
.
toHtml
()
+
"</body></html>"
)
.
build
();
}
catch
(
DBException
e
)
{
return
Response
.
serverError
()
.
entity
(
"<!DOCTYPE html>\n<html><body><p>WIELKA BIEDA</p></body></html>"
)
.
build
();
}
catch
(
RecordNotFound
e
)
{
return
Response
.
status
(
404
)
.
entity
(
"<!DOCTYPE html>\n<html><body><p>BRAK TAKIEGO PRODUKTU</p></body></html>"
)
.
build
();
}
}
}
PC27-RestSerwer/src/main/java/sklep/rest/ext/RecordNotFoundMapper.java
0 → 100644
View file @
36dd92e3
package
sklep
.
rest
.
ext
;
import
jakarta.ws.rs.core.Response
;
import
jakarta.ws.rs.ext.ExceptionMapper
;
import
jakarta.ws.rs.ext.Provider
;
import
sklep.db.RecordNotFound
;
// Gdy taka klasa jest obecna w projekcie, to w każdej sytuacji, gdy metoda "restowa" kończy się wyjątkiem
// RecordNotFound, serwer odeśle odpowiedź przygotowaną przez tego mappera.
@Provider
public
class
RecordNotFoundMapper
implements
ExceptionMapper
<
RecordNotFound
>
{
@Override
public
Response
toResponse
(
RecordNotFound
exception
)
{
String
html
=
"""
<html><body>
<h1>Nie znaleziono</h1>
<p style='color:red'>
"""
+
exception
.
getMessage
()
+
"</p></body></html>"
;
return
Response
.
status
(
404
)
.
type
(
"text/html"
)
.
entity
(
html
)
.
build
();
}
}
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