英文:
Adding fields to response json in spring boot get method
问题
我有一个从仓库向REST控制器提供数据的服务:
@Service
public class TaskServiceImpl implements TaskService {
@Autowired
private TaskRepository taskRepository;
@Override
public List<Task> getAllTasks() {
return taskRepository.findAll();
}
}
还有一个REST控制器:
@RestController
@RequestMapping("/tasks")
public class TaskController {
@Autowired
private TaskService taskService;
@GetMapping
public List<Task> getAllTasks() {
return taskService.getAllTasks();
}
}
我的任务是不仅返回所有任务,还要返回两个字段 - 待办任务数和已完成任务数。我知道如何从数据库中获取这个计数。但是如何将这些字段添加到响应JSON中呢?响应JSON必须如下所示:
{
"tasks": [
{
"createTime": null,
"updateTime": null,
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"title": "待办任务",
"description": "blabla",
"priority": "HIGH",
"done": false
},
{
"createTime": null,
"updateTime": null,
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"title": "已完成任务",
"description": "blabla",
"priority": "HIGH",
"done": true
}
],
"todoCount": 1,
"doneCount": 1
}
英文:
I have the service that gives data from repository to rest controller:
@Service
public class TaskServiceImpl implements TaskService {
@Autowired
private TaskRepository taskRepository;
@Override
public List<Task> getAllTasks() {
return taskRepository.findAll();
}
}
And also rest controller:
@RestController
@RequestMapping("/tasks")
public class TaskController {
@Autowired
private TaskService taskService;
@GetMapping
public List<Task> getAllTasks() {
return taskService.getAllTasks();
}
}
My task is to return not only all the tasks but two fields two - todo tasks count and ready tasks count. I know how find this count from db. But what is the proper way to add this fields to response json? Response json must look like:
{
[
{
"createTime": null,
"updateTime": null,
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"title": "todo-task",
"description": "blabla",
"priority": "HIGH",
"done": false,
},
{
"createTime": null,
"updateTime": null,
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"title": "done-task",
"description": "blabla",
"priority": "HIGH",
"done": true,
}
],
todoCount: 1,
doneCount: 1
}
答案1
得分: 0
你可以通过创建一个新的模型,由Controller返回,并设置你的todoCount和doneCount值:
@GetMapping
public TasksModel getAllTasks() {
// 获取 todoCount 和 doneCount 值
TasksModel tasksModel = new TasksModel();
tasksModel.setTaskModelList(taskService.getAllTasks());
tasksModel.setTodoCount(todoCount);
tasksModel.setDoneCount(doneCount);
return tasksModel;
}
而TasksModel的定义如下:
class TasksModel {
List<Task> taskModelList;
int todoCount;
int doneCount;
// getter
// setter
}
英文:
you can do that by creating new model to be returned by the Controller and set your todoCount and doneCount values:
@GetMapping
public TasksModel getAllTasks() {
// get todoCount and doneCount values
TasksModel tasksModel = new TasksModel();
tasksModel.setTaskModelList(taskService.getAllTasks())
tasksModel.setTodoCount(todoCount);
tasksModel.setDoneCount(doneCount);
return taskModel;
}
and TasksModel is :
class TasksModel {
List<Task> taskModelList;
int todoCount;
int doneCount;
//getter
//setter
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论