postman “缺少必需的请求正文:〈?>我的博客是关于如何使用这个框架的,

9rygscc1  于 2022-11-07  发布在  Postman
关注(0)|答案(3)|浏览(315)

我正在学习Spring-Boot的特性,也在Postman上学习过。但是当我运行代码时,我在Postman上得到了400个错误请求,如下所示:“缺少必需的请求正文:是我遗漏了一个属性还是编码一路出错了?

@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() {

}

此处为Postman request

50few1ms

50few1ms1#

Redi这里有一个请求示例:

curl -X POST \
  'http://>>>> your endpoint url' \
  -H 'Content-Type: application/json' \
  -d '{
"summary" : "sample summary",
"acceptanceCriteria" : "blabla",
"status" : "fake status"
}'
sf6xfgos

sf6xfgos2#

因为它是一个@PostMapping,所以您需要发出一个 Postman POST请求,如下所示:

确保选择rawapplication/json的内容类型,沿着请求主体的json对象

yruzcnhs

yruzcnhs3#

是的,你发送了一个错误的请求。你必须在POST请求中发送请求负载。

**解决方案:**发送POST请求时,请根据您得情况发送这样得正文.

{
"summary":"any",
"acceptance":"any",
"status":"any"
}

相关问题