Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: return @ResponseBody "ResponseEntity<List<JSONObject>>"

In controller I create json array. If I return List<JSONObject> it is ok:

@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; } 

but I need to return JSON array and HTTP status code:

@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 see error in XXX line:

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 

How can I return json+http reply? There is my working code for returning one json object + http status code:

@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); } 
like image 533
martin Avatar asked Oct 11 '14 22:10

martin


People also ask

What is the return type of ResponseEntity?

A ResponseEntity is returned. We give ResponseEntity a custom status code, headers, and a body. With @ResponseBody , only the body is returned. The headers and status code are provided by Spring.

What does ResponseEntity OK () do?

ResponseEntity represents the whole HTTP response: status code, headers, and body. As a result, we can use it to fully configure the HTTP response.

What is ResponseEntity status HttpStatus?

ResponseEntity is meant to represent the entire HTTP response. You can control anything that goes into it: status code, headers, and body. @ResponseBody is a marker for the HTTP response body and @ResponseStatus declares the status code of the HTTP response.


2 Answers

Instead of

return new ResponseEntity<JSONObject>(entities, HttpStatus.OK); 

try

return new ResponseEntity<List<JSONObject>>(entities, HttpStatus.OK); 
like image 166
user2428804 Avatar answered Sep 22 '22 01:09

user2428804


Now I return Object. I don't know better solution, but it works.

@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); } 
like image 27
martin Avatar answered Sep 21 '22 01:09

martin