且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 JSON 发布到控制器返回 400 错误请求

更新时间:2023-09-01 11:24:34

从你给出的解释中可以清楚地看出,你错误地使用了 POSTMAN.

From the explanation you have given one thing is clear, You are using POSTMAN wrongly.

您不能在 form-data 中发送带有 @RequestBody 注释的对象.使用 @ModelAttribute@RequestParam(多个)处理表单.

You can not send a @RequestBody annotated object in a form-data. Forms are handled using @ModelAttribute or @RequestParam (Multiple of this).

如果您想将 JSON 发布到控制器端点,您必须像下图那样操作;

If you want to post a JSON to an Controller end point you must do like the following image;

显然你必须选择POST并且必须设置一个标题Content-Typeapplication/json,最后你必须选择raw 从选项卡中选择 JSON 从下拉列表中.

You must select POST obviously and must set a header Content-Type as application/json, finally you must select raw from the tabs and select JSON from the dropdown.

如果你遵循这个你的控制器方法必须使用 Todo 对象作为 @RequestBody 注释并且不需要使用 String 并手动转换为对象.

If you follow this your Controller method must work with Todo object as @RequestBody annotation and no need to use String and manually convert to object.