我将Task
实体表示为:
@Entity
@Getter
@NoArgsConstructor
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NotBlank
private String name;
@Enumerated(EnumType.STRING)
private Category category;
@OneToOne
private Employee assignee;
@OneToMany
private List<Task> precedingTasks;
private String details;
private int estimatedTimeInMinutes;
private LocalDateTime deadline;
private LocalDateTime creationTime;
private LocalDateTime startTime;
private LocalDateTime endTime;
public Task(String name, Category category, Employee assignee, List<Task> precedingTasks, String details,
int estimatedTimeInMinutes, LocalDateTime deadline) {
this.name = name;
this.category = category;
this.assignee = assignee;
this.precedingTasks = precedingTasks;
this.details = details;
this.estimatedTimeInMinutes = estimatedTimeInMinutes;
this.deadline = deadline;
this.creationTime = LocalDateTime.now();
this.startTime = null;
this.endTime = null;
}
我注意到正确添加了没有precedingTasks
的任务,但是没有正确添加。
例如:添加三个没有precedingTasks
的任务后,我按以下方式在邮递员中发送POST
:
{
"name": "Wyslac przesyłke nr 87",
"category": "TODO",
"assigneeId": 1,
"precedingTasksId": [1, 2, 3],
"details": "Dodac zakladke",
"estimatedTimeInMinutes": 78,
"deadline": "2013-12-18T14:30:40.100000000"
}
但是我得到的任务没有preceding tasks
:
{
"id": 4,
"name": "Wyslac przesyłke nr 87",
"category": "TODO",
"assignee": {
"id": 1,
"firstName": "Wojciech",
"lastName": "Wasilewski",
"email": "wojtek@wasilewski.pl",
"role": "ADMIN",
"password": "h*n$GvFE",
"manager": true,
"password_LENGTH": 8,
"passwordValid": false
},
"precedingTasks": [],
"details": "Dodac zakladke",
"estimatedTimeInMinutes": 78,
"deadline": "2013-12-18T14:30:40.1",
"creationTime": "2018-08-01T13:38:45.924",
"startTime": null,
"endTime": null
}
我使用此控制器:
@PostMapping("/tasks")
@ResponseStatus(HttpStatus.CREATED)
public Task addNewTask(@RequestBody TaskRequest taskRequest) {
String name = taskRequest.getName();
Category category = taskRequest.getCategory();
checkIfAssigneeExists(taskRequest.getAssigneeId());
Employee assignee = employeeRepository.findById(taskRequest.getAssigneeId()).get();
List<Task> precedingTasks = new ArrayList<>();
if (taskRequest.getPrecedingTaskIds() != null)
taskRequest.getPrecedingTaskIds().forEach(id -> precedingTasks.add(taskRepository.findById(id).get()));
String details = taskRequest.getDetails();
int estimatedTimeInMinutes = taskRequest.getEstimatedTimeInMinutes();
LocalDateTime deadline = taskRequest.getDeadline();
Task task = new Task(name, category, assignee, precedingTasks, details, estimatedTimeInMinutes, deadline);
taskRepository.save(task);
return task;
}
这样的请求:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TaskRequest {
@NonNull
private String name;
@NonNull
private Category category;
@NonNull
private Long assigneeId;
@NonNull
private List<Long> precedingTaskIds;
private String details;
private int estimatedTimeInMinutes;
@NonNull
private LocalDateTime deadline;
}
怎么了?是JSON还是ArrayList的格式?在添加整个任务之前,我应该在数据库中添加precedingTasks
吗?
答案 0 :(得分:2)
类 TaskRequest 中的字段 precedingTaskIds 与名称不对应于POST请求 precedingTasksId 中的名称。重命名它或重写Postman中的POST。
答案 1 :(得分:1)
您发布“ precedingTasksId”,但该类需要“ precedingTasksIds”。
好像只是一个错字。
让我知道是否可行。