Spring: return @ResponseBody "ResponseEntity<List<JSONObject>>"
Asked Answered
O

5

41

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);
}
Outsert answered 11/10, 2014 at 22:55 Comment(3)
I think it should've just been return new ResponseEntity<List<JSONObject>>(entities, HttpStatus.OK); right? Regardless, your solution below is fine.Emetic
Thanks, it works. What is better? ResponseEntity<List<JSONObject>> or ResponseEntity<Object> What is your opinion?Outsert
For readability concerns, the former is better in my opinion.Emetic
O
28

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);
}
Outsert answered 11/10, 2014 at 23:35 Comment(3)
Other solution is use https://mcmap.net/q/392841/-dynamically-changing-the-responsestatus-in-annotation-driven-spring-mvc @ResponseStatus and @ExceptionHandler.Outsert
ResponseEntity<Object> works for an obvious reason, because Object is parent class for everythingPollux
I tried the same (return new ResponseEntity<Object>(entities, HttpStatus.OK);)but the controller returning empty array for me. Any suggestions pleasePose
S
36

Instead of

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

try

return new ResponseEntity<List<JSONObject>>(entities, HttpStatus.OK);
Smashed answered 3/2, 2015 at 15:45 Comment(0)
O
28

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);
}
Outsert answered 11/10, 2014 at 23:35 Comment(3)
Other solution is use https://mcmap.net/q/392841/-dynamically-changing-the-responsestatus-in-annotation-driven-spring-mvc @ResponseStatus and @ExceptionHandler.Outsert
ResponseEntity<Object> works for an obvious reason, because Object is parent class for everythingPollux
I tried the same (return new ResponseEntity<Object>(entities, HttpStatus.OK);)but the controller returning empty array for me. Any suggestions pleasePose
E
14

Personally, I prefer changing the method signature to:

public ResponseEntity<?>

This gives the advantage of possibly returning an error message as single item for services which, when ok, return a list of items.

When returning I don't use any type (which is unused in this case anyway):

return new ResponseEntity<>(entities, HttpStatus.OK);
Esquiline answered 21/10, 2016 at 10:22 Comment(0)
B
0

I have no idea why the other answers didn't work for me (error 500) but this works

@GetMapping("")
public String 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.toString();
}
Buckboard answered 30/4, 2020 at 13:24 Comment(0)
C
-3

I am late for this but i want put some more solution relevant to this.

 @GetMapping
public ResponseEntity<List<JSONObject>> getRole() {
    return ResponseEntity.ok(service.getRole());
}
Carrot answered 21/2, 2020 at 13:21 Comment(1)
I don't see any reference to a service or role in the original question, and besides, how would you get each JSONObjects role without iterating through the list of JSONObjects?Gauzy

© 2022 - 2024 — McMap. All rights reserved.