在Spring Boot的GET方法中向响应JSON添加字段。

huangapple go评论75阅读模式
英文:

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&lt;Task&gt; getAllTasks() {
        return taskRepository.findAll();
    }
}

And also rest controller:
@RestController
@RequestMapping("/tasks")
public class TaskController {
@Autowired
private TaskService taskService;

    @GetMapping
    public List&lt;Task&gt; 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:

{
  [
    {
      &quot;createTime&quot;: null,
      &quot;updateTime&quot;: null,
      &quot;id&quot;: &quot;3fa85f64-5717-4562-b3fc-2c963f66afa6&quot;,
      &quot;title&quot;: &quot;todo-task&quot;,
      &quot;description&quot;: &quot;blabla&quot;,
      &quot;priority&quot;: &quot;HIGH&quot;,
      &quot;done&quot;: false,
    },
    {
      &quot;createTime&quot;: null,
      &quot;updateTime&quot;: null,
      &quot;id&quot;: &quot;3fa85f64-5717-4562-b3fc-2c963f66afa6&quot;,
      &quot;title&quot;: &quot;done-task&quot;,
      &quot;description&quot;: &quot;blabla&quot;,
      &quot;priority&quot;: &quot;HIGH&quot;,
      &quot;done&quot;: 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&lt;Task&gt; taskModelList;
     int todoCount;
     int doneCount;

//getter
//setter

}

huangapple
  • 本文由 发表于 2020年10月24日 18:31:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64512397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定