Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
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_20230617
Commits
bb07f89c
Commit
bb07f89c
authored
Jul 30, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pierwszy REST
parent
ed364b86
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
0 deletions
+57
-0
notatka_rest.txt
PC27-SklepSpring/notatka_rest.txt
+21
-0
ProductEndpoint.java
...SklepSpring/src/main/java/sklep/rest/ProductEndpoint.java
+35
-0
SecurityConfig.java
...epSpring/src/main/java/sklep/security/SecurityConfig.java
+1
-0
No files found.
PC27-SklepSpring/notatka_rest.txt
0 → 100644
View file @
bb07f89c
REST - REpresentational State Transfer
"usługa REST" to serwer HTTP, który odpowiada na żądania klientów
usługa działa na danych (w formacie JSON, czasami XML lub innym), a nie służy do tego, aby tworzyć ładnie wyglądające stronki
serwer daje zdalny dostęp do danych, do tzw. 'zasobów' / resource
klient za pomocą zapytania GET może odczytać zasób wskazywany przez URL,
za pomocą PUT może zapisać zasób na serwerze (o ile konkretna aplikacja taką funkcjonalność obejmuje)
za pomocą DELETE można zasób usunąć
operacja POST służy do:
dodawania nowych zasobów o nieznanym jeszcze ID
do wszelkich innych operacji nie mieszczących się w logice GET/PUT/DELETE
Zastosowania:
udostępnianie danych i usług użytkownikom / innym partnerom
zaplecze dla aplikacji webowych tworzonych w oparciu o JS
zaplecze dla aplikacji moblinych
Skrypt JS lub aplikacja mobilna wysyła zapytania na serwer RESTowy, aby pobrać jakieś dane, aby coś zapisać.
Serwer REST-owych może być utworzony w Javie, w Pythonie, w JS (Node), PHP, ...
Z tym serwerem mogą się komunikować aplikacje napisane też w różnych językach.
PC27-SklepSpring/src/main/java/sklep/rest/ProductEndpoint.java
0 → 100644
View file @
bb07f89c
package
sklep
.
rest
;
import
java.util.List
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.server.ResponseStatusException
;
import
sklep.model.Product
;
import
sklep.repository.ProductRepository
;
@Controller
@RequestMapping
(
"/rest/products"
)
public
class
ProductEndpoint
{
@Autowired
private
ProductRepository
productRepository
;
@GetMapping
@ResponseBody
public
List
<
Product
>
readAll
()
{
return
productRepository
.
findAll
();
}
@GetMapping
(
"/{id}"
)
@ResponseBody
public
Product
readOne
(
@PathVariable
Integer
id
)
{
return
productRepository
.
findById
(
id
).
orElseThrow
(()
->
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
));
}
}
PC27-SklepSpring/src/main/java/sklep/security/SecurityConfig.java
View file @
bb07f89c
...
...
@@ -43,6 +43,7 @@ public class SecurityConfig {
.
requestMatchers
(
"/products/**"
).
permitAll
()
// kolejność reguł ma znaczenie
.
requestMatchers
(
"/customers/new"
,
"/customers/*/edit"
).
hasRole
(
"manager"
)
// skrót na hasAuthority("ROLE_...")
.
requestMatchers
(
"/customers/**"
).
authenticated
()
.
requestMatchers
(
"/rest/**"
).
permitAll
()
.
anyRequest
().
denyAll
()
// dobra praktyka - odrzucanie pozostałych zapytań; Spring domyślnie wymagałby "authenticated"
).
formLogin
(
Customizer
.
withDefaults
())
;
...
...
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