我正在学习Spring-Boot功能,并与Postman一起使用它们。但是当我运行代码时,我在邮递员上收到400 Bad Request Error,如下所示: “缺少所需的请求正文:public org.springframework.http.ResponseEntity”。我是否错过了属性或编码完全错误?
@RestController
@RequestMapping("/api/board")
@CrossOrigin
public class ProjectTaskController {
@Autowired
private ProjectTaskService projectTaskService;
@PostMapping("")
public ResponseEntity<?> addPTToBoard( @Valid @RequestBody ProjectTask projectTask, BindingResult result){
if(result.hasErrors()) {
Map<String, String> errorMap= new HashMap<>();
for(FieldError error: result.getFieldErrors()) {
errorMap.put(error.getField(), error.getDefaultMessage());
}
return new ResponseEntity<Map<String, String>>(errorMap, HttpStatus.BAD_REQUEST);
}
ProjectTask newPT= projectTaskService.saveOrUpdateProjectTask(projectTask);
return new ResponseEntity<ProjectTask>(newPT, HttpStatus.CREATED);
}
projectTask代码,它也具有getter和setter。
@Entity
public class ProjectTask {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private long id;
@NotBlank(message = "summary cannot be blank")
private String summary;
private String acceptanceCriteria;
private String status;
public ProjectTask() {
}
答案 0 :(得分:0)
答案 1 :(得分:0)
Redi这是一个示例请求:
curl -X POST \
'http://>>>> your endpoint url' \
-H 'Content-Type: application/json' \
-d '{
"summary" : "sample summary",
"acceptanceCriteria" : "blabla",
"status" : "fake status"
}'
答案 2 :(得分:0)
是的..!是的,您发送的请求不正确。您必须在POST请求中发送请求有效负载。
解决方案: 在发送POST请求时,请以您的情况随身携带。
{
"summary":"any",
"acceptance":"any",
"status":"any"
}