-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_java_rest_L_parametres.qmd
More file actions
167 lines (143 loc) · 5.12 KB
/
Copy path05_java_rest_L_parametres.qmd
File metadata and controls
167 lines (143 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
---
title: "Paramètres et annotations JAX-RS"
description: |
Gestion des paramètres dans les services REST Jakarta.
- Paramètres de chemin (@PathParam)
- Paramètres de requête (@QueryParam)
- Paramètres de formulaire (@FormParam)
- En-têtes HTTP (@HeaderParam)
- Corps de requête (@Consumes)
- Validation des paramètres
categories:
- Java
- Lecture
- I211
- RESTful
- Web Services
provide_notes: true
provide_slides: false
jupyter: java
execute:
error: true
---
## Les paramètres complexes dans les corps de requêtes
Jakarta REST facilite la manipulation des données complexes en automatisant la conversion entre les formats d'échange (JSON, XML) et les objets Java. Cette conversion bidirectionnelle s'applique particulièrement aux corps des requêtes HTTP (PUT, POST), où les données reçues sont automatiquement transformées en paramètres Java utilisables dans vos méthodes.
L'annotation `@Consumes` joue un rôle central dans ce processus en spécifiant les types MIME acceptés par votre endpoint. Par exemple, dans la méthode `addAuthor()`, elle indique les formats acceptables pour la création d'un auteur. Le client précise le format effectivement utilisé via l'en-tête HTTP `Content-Type`, et le serveur retourne l'entité complète, y compris les champs générés comme l'ID.
```{java}
//| echo: false
//| output: asis
%%shell
PROVIDER="github"
REPO="ebpro/notebook-java-rest-sample-jakartarestfull"
BRANCH="develop"
gitpull.sh --provider github \
--branch ${BRANCH} ${REPO} \
--message "Les exemples suivants sont accessibles dans le dépôt :"
```
```{java}
//| echo: false
//| output: false
%%shell
PROVIDER="github"
REPO="ebpro/notebook-java-rest-sample-jakartarestfull"
BRANCH="develop"
source get_src_dir.sh ${PROVIDER} ${REPO}
cd ${SRC_DIR}
./mvnw --quiet verify
```
```{java}
//| echo: false
//| output: false
String SRC_DIR="/home/jovyan/work/materials/github/ebpro/notebook-java-rest-sample-jakartarestfull";
%jars "/home/jovyan/work/materials/github/ebpro/notebook-java-rest-sample-jakartarestfull/target/sample-jaxrs-*-withdependencies.jar";
```
```{java}
//| output: false
//| echo: false
//| vscode: {languageId: java}
import org.glassfish.grizzly.http.server.HttpServer;
import fr.univtln.bruno.samples.jaxrs.server.BiblioServer;
HttpServer httpServer = BiblioServer.startServer();
httpServer.toString();
```
```{java}
//| output: true
//| echo: false
//| vscode: {languageId: java}
// PRINT CLASS
String script="/home/jovyan/work/materials/github/ebpro/notebook-java-rest-sample-jakartarestfull/src/main/java/fr/univtln/bruno/samples/jaxrs/resources/AuthorResource.java";
IJava.getKernelInstance().getMagics().applyCellMagic("javasrcMethodByName",List.of("AuthorResource","addAuthor"),script);
return null;
```
Adds an author :
```{java}
//| vscode: {languageId: java}
%%shell
curl -s -i -H "Accept: application/json" \
-H "Content-type: application/json" \
-X POST \
-d '{"name":"John","firstname":"Smith","biography":"My life"}' \
http://localhost:9998/mylibrary/authors
```
La méthode `updateAuteur` est appelée par un PUT mais avec une resource précise (indiquée dans l'URL) à mettre à jour.
```{java}
//| output: true
//| echo: false
//| vscode: {languageId: java}
// PRINT CLASS
String script="/home/jovyan/work/materials/github/ebpro/notebook-java-rest-sample-jakartarestfull/src/main/java/fr/univtln/bruno/samples/jaxrs/resources/AuthorResource.java";
IJava.getKernelInstance().getMagics().applyCellMagic("javasrcMethodByName",List.of("AuthorResource","updateAuthor"),script);
return null;
```
Fully update an author
```{java}
//| vscode: {languageId: java}
%%shell
curl -s -i -H "Accept: application/json" \
-H "Content-type: application/json" \
-X PUT \
-d '{"name":"Martin","firstname":"Jean","biography":"ma vie"}' \
http://localhost:9998/mylibrary/authors/1
```
### La suppression
La suppression des ressources se fait avec les approches précédentes.
```{java}
//| output: true
//| echo: false
//| vscode: {languageId: java}
// PRINT CLASS
String script="/home/jovyan/work/materials/github/ebpro/notebook-java-rest-sample-jakartarestfull/src/main/java/fr/univtln/bruno/samples/jaxrs/resources/AuthorResource.java";
IJava.getKernelInstance().getMagics().applyCellMagic("javasrcMethodByName",List.of("AuthorResource","removeAuthor"),script);
return null;
```
Removes one author :
```{java}
//| slideshow: {slide_type: skip}
//| tags: []
//| vscode: {languageId: java}
%%shell
curl -s -i -X DELETE \
http://localhost:9998/mylibrary/authors/1
```
```{java}
//| output: true
//| echo: false
//| vscode: {languageId: java}
// PRINT CLASS
String script="/home/jovyan/work/materials/github/ebpro/notebook-java-rest-sample-jakartarestfull/src/main/java/fr/univtln/bruno/samples/jaxrs/resources/AuthorResource.java";
IJava.getKernelInstance().getMagics().applyCellMagic("javasrcMethodByName",List.of("AuthorResource","removeAuthors"),script);
return null;
```
Removes all authors
```{java}
//| vscode: {languageId: java}
%%shell
curl -s -i -X DELETE \
http://localhost:9998/mylibrary/authors/
```
```{java}
//| output: false
//| echo: false
//| vscode: {languageId: java}
httpServer.stop();
```