我用一个简单的post和get方法编写了一个非常简单的控制器类。
今天是我第一次使用Postman-我不知道自己在做什么错,但是每次使用 POST 映射,我都会得到:
{
"timestamp": "2020-03-29T19:05:49.548+0000",
"status": 400,
"error": "Bad Request",
"message": "Required request body is missing: public int pl.futurecollars.controller.GreetingControler.saveGreeting(pl.futurecollars.controller.Greeting)",
"path": "/greetings"
}
控制器类:
@RestController
@RequestMapping("/greetings")
public class GreetingControler {
private Map<Integer, Greeting> greetings = new HashMap<>();
private int counter = 0;
@GetMapping
public Collection<Greeting> getGreetings() {
return greetings.values();
}
@PostMapping
public int saveGreeting(@RequestBody Greeting greeting){
greeting.setId(counter);
greetings.put(counter, greeting);
return counter++;
}
@DeleteMapping("/{id}")
public void deleteGreeting(@PathParam("id") int greetingId){
greetings.remove(greetingId);
}
}
我添加了一个@RequestBody
,但它仍然说它丢失了。
如何解决此问题?
答案 0 :(得分:0)
首先根据注释中提到的错误验证您的输入,其中一个字段名以.
开头,而应该在""
中。您可以在https://jsoneditoronline.org/上验证json。
还添加邮递员快照,仅供参考
Inside postman -> Body where u can put payload
答案 1 :(得分:0)
该错误消息对丢失的内容有明确的描述:
缺少所需的请求正文:...
因此,您错过了向请求添加主体的操作。
您的配置应如下所示:
主体应完全是Greeting
类的JSON对象。
应该将其发布到raw
标签上。
此外,请不要忘记header
上的 Content-Type :
有用的链接: