Junior — Middle
What data class should be used in ResponseBody when the exact content of the response is unknown?
sobes.tech AI
Answer from AI
If the exact content of the response is unknown, in Java, ResponseBody typically uses the Object class or generic data structures such as Map<String, Object>.
Example using Map for a JSON response with an unknown structure:
@RestController
public class MyController {
@GetMapping("/data")
public ResponseEntity<Map<String, Object>> getData() {
Map<String, Object> response = new HashMap<>();
response.put("key1", "value1");
response.put("key2", 123);
// You can dynamically add any data
return ResponseEntity.ok(response);
}
}
Using Object or Map allows flexible handling of different data structures without strict typing.