Dans le contrôleur, je crée un tableau JSON. Si je reviens List<JSONObject>
c'est bon:
@RequestMapping(value="", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<JSONObject> getAll() {
List<Entity> entityList = entityManager.findAll();
List<JSONObject> entities = new ArrayList<JSONObject>();
for (Entity n : entityList) {
JSONObject entity = new JSONObject();
entity.put("id", n.getId());
entity.put("address", n.getAddress());
entities.add(entity);
}
return entities;
}
mais je dois renvoyer le tableau JSON et le code d'état HTTP:
@RequestMapping(value="", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<List<JSONObject>> getAll() {
List<Entity> entityList = entityManager.findAll();
List<JSONObject> entities = new ArrayList<JSONObject>();
for (Entity n : entityList) {
JSONObject Entity = new JSONObject();
entity.put("id", n.getId());
entity.put("address", n.getAddress());
entities.add(entity);
}
return new ResponseEntity<JSONObject>(entities, HttpStatus.OK); // XXX
}
Eclipse voir erreur dans la ligne XXX:
Multiple markers at this line
- The constructor ResponseEntity<JSONObject>(List<JSONObject>, HttpStatus) is undefined
- Type mismatch: cannot convert from ResponseEntity<JSONObject> to
ResponseEntity<List<JSONObject>>
- Type mismatch: cannot convert from ResponseEntity<JSONObject> to JSONObject
Comment puis-je renvoyer json + réponse http? Il y a mon code de travail pour renvoyer un objet json + code de statut http:
@RequestMapping(value="/{address}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<JSONObject> getEntity(@PathVariable("address") int address) {
Entity n = entityManager.findByAddress(address);
JSONObject o = new JSONObject();
o.put("id", n.getId());
o.put("address", n.getAddress());
return new ResponseEntity<JSONObject>(o, HttpStatus.OK);
}
Maintenant je retourne Object
. Je ne connais pas de meilleure solution, mais ça marche.
@RequestMapping(value="", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<Object> getAll() {
List<Entity> entityList = entityManager.findAll();
List<JSONObject> entities = new ArrayList<JSONObject>();
for (Entity n : entityList) {
JSONObject Entity = new JSONObject();
entity.put("id", n.getId());
entity.put("address", n.getAddress());
entities.add(entity);
}
return new ResponseEntity<Object>(entities, HttpStatus.OK);
}
Au lieu de
return new ResponseEntity<JSONObject>(entities, HttpStatus.OK);
essayer
return new ResponseEntity<List<JSONObject>>(entities, HttpStatus.OK);
Personnellement, je préfère changer la signature de la méthode pour:
public ResponseEntity<?>
Cela donne l’avantage de renvoyer éventuellement un message d’erreur en tant qu’élément unique pour les services qui, lorsque cela est correct, renvoient une liste d’éléments.
Lors du retour, je n'utilise aucun type (qui n'est pas utilisé dans ce cas):
return new ResponseEntity<>(entities, HttpStatus.OK);