mrwhiteblue
mrwhiteblue

Reputation: 3

Dynamically creatre response structure

I want to dynamically create a specific response in my controller.

    @GetMapping("/test")
    public ResponseEntity<?> getLanguageList() {
        return ResponseEntity.ok(new Object() ??
);
    }

And response on GET /test should be like that:

{
 status: "OK",
 info: "Hello"
}

How to do that? I don't want to create a new class for response.

Upvotes: 0

Views: 37

Answers (1)

devReddit
devReddit

Reputation: 2947

Return a Map<String, Object> or Map<String, String> in the ResponseEntity. You can construct and add properties as you need in runtime to a map and it will be converted to a json structure by `ResponseEntity.ok. So, just do:

return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK) ;

Upvotes: 1

Related Questions