You will need to use records. Here’s an example of it:

public record TaskResponse(String message, List<Task> task) {}

here’s its implementation:

@GetMapping("/tasks") 
public ResponseEntity<TaskResponse> index() {
	List<Task> tasks = repository.findAll();
	TaskResponse response = new TaskResponse("Success", tasks);
	return ResponseEntity.status(HttpStatus.OK).body(response);
}