Bonjour à tous !
Alors voila, je suis en train de développer le serveur d'une application JAVA WEB basée sur une architecture RESTful.
J'ai mon controller :
Mon objet ReportDto (simplifié au maximum) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/report") public class ReportController { @RequestMapping(value="", method=RequestMethod.POST, consumes={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}, produces={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) public ResponseEntity<ReportDto> ListsGet(@RequestBody ReportDto report) { ResponseEntity<ReportDto> response = null; response = new ResponseEntity<ReportDto>(report, HttpStatus.OK); return response; } }
Maintenant quand j'utilise le client REST de mozilla (chrome://restclient/content/restclient.html), que je choisi :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlAccessType; @XmlRootElement(name="Report") @XmlAccessorType(XmlAccessType.FIELD) public class ReportDto implements Serializable { private static final long serialVersionUID = 1L; @XmlElement(name = "Title") private String title; @XmlElement(name = "Id") private int id; public String getTitle() { return this.title; } public int getId() { return this.id; } public void setTitle(String title) { this.title = title; } public void setId(int id) { this.id = id; } @Override public String toString() { return "\n######################\n" + "Title = [" + this.title + "]\n" + "Id = [" + this.id + "]" + "\n######################\n"; } }
Method = POST
URL = http://localhost:8080/monProjet/api/...esources/list/
Header = Content-Type : application/xml
Body =Et que je send, j'ai bien le mapping qui se fait dans mon controller, j'affiche mon objet JAVA et je le reçois même coté REST client.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4<Report> <Title>TEST</Title> <Id>3</Id> </Report>
Maintenant ce que j'essaye de faire c'est la même chose mais avec du JSON
Sur mon REST client je choisi :
Method = POST
URL = http://localhost:8080/monProjet/api/...esources/list/
Header = Content-Type : application/json
Body =Et quand je send j'ai une erreur 400 Mauvaise Requête.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 { "ReportDto": { "Title": "Test", "Id": 4 } }
Ensuite j'ai vu qu'il fallait ajouter un mapper JSON donc j'ai rajouté dans mon pom.xml :
Mais ça ne change rien..
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.3.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.3.2</version> </dependency>
Si quelqu'un pouvait me dire ce que je fais de travers, ou ce que j'ai oublié de faire cela m'aiderait beaucoup. Je reste à disposition pour répondre à toutes question si jamais je n'avais pas été assez précis.
Merci d'avance.
Partager