尝试发出发布请求时,“缺少 HttpMessageNotReadableException 必需的请求正文”

闪电吉米乔约翰逊

我有一个带有几个方法的控制器类,其中一个方法是应该接受 POST 请求并使用来自该 POST 请求正文的 JSON 创建一个新帐户的方法。

当我尝试使用 curl 发出 POST 请求时,我收到一个错误消息

{"timestamp":1493988808871,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Required request body is missing: org.springframework.http.ResponseEntity<?> com.example.AccountRestController.add(java.lang.String,java.lang.String)","path":"/users/add"}

我正在使用的 curl 命令

curl -X POST --data '{"userName":"bepis", "password":"xyz"}' -H "Content-Type:application/json" http://localhost:8080/users/add

帐户休息控制器

@RequestMapping(method = RequestMethod.POST, value = "/add", produces = { MediaType.APPLICATION_JSON_VALUE})
ResponseEntity<?> add(@RequestBody String username, @RequestBody String password) {
    Account result = accountRepository.save(new Account (username, password));
    return new ResponseEntity<>(result, HttpStatus.CREATED);
}
安德烈

您不能使用多个@RequestBody. 您需要将所有内容包装到一个用于匹配您的请求正文的类中。

这里也有同样的回答

对于被拒绝的功能请求,还有一个JIRA 问题

注意:如果你想写得更少,你可以使用@PostMapping代替@RequestMapping(method = RequestMethod.POST).

注意: @RequestParam and@PathVariable用于从 URI 中提取数据,而不是从正文中提取数据。

注意:这同样适用于[FromBody]来自的等效属性ASP.NET WebAPI

完整示例:

波纹管我创建了一个类似于您的案例的工作示例:

请求 DTO

public class AccountCreateRequest {

    private String userName;
    private String password;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

响应 DTO

public class AccountCreateResponse {

    private String userName;
    private String password;

    public AccountCreateResponse() {
    }

    public AccountCreateResponse(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

控制器

@RestController
@RequestMapping("/v1/account")
public class AccountController {

    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseStatus(HttpStatus.CREATED) AccountCreateResponse add(@RequestBody() AccountCreateRequest account) {
        AccountCreateResponse response = new AccountCreateResponse(account.getUserName(), account.getPassword());
        return response;
    }
}

卷曲请求

curl -X POST --data '{"userName":"bepis", "password":"xyz"}' -H "Content-Type:application/json" http://localhost:8080/v1/account

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

尝试发出发布请求时,Spring Boot“ HttpMessageNotReadableException缺少所需的请求正文”

Spring框架HttpMessageNotReadableException:缺少所需的请求正文

无法读取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException:缺少所需的请求正文

缺少必需的请求正文-HTTP删除

通过HTML表单使用请求正文发出发布请求

Spring Boot发布休息呼叫“缺少必需的请求正文”

发出发布请求的问题

发送POST请求时org.springframework.http.converter.HttpMessageNotReadableException

vscode /崇高文本在保存时发出发布请求

发出发布请求时Vue.js axios错误

在发出发布请求时无法从jsoup获得结果

向logstash发出发布请求时发生CORS错误

发出发布请求时为WebClient设置主体

在Python上发出发布请求时,如何调用函数?

发出发布请求时,状态未正确更新

使用Axios发出发布请求时出现网络错误

发送POST请求或尝试更新时出现org.springframework.http.converter.HttpMessageNotReadableException

使用HttpServletRequestWrapper复制后缺少必需的请求正文

使用urllib发出发布请求

如何通过AFnetworking发出发布请求?

如何使用OKHTTP发出发布请求?

如何使用SwiftyJSON发出发布请求

无法在Android中发出发布请求

在Golang中发出发布请求

通过angularjs发出发布请求后尝试从Node JS渲染页面

使用请求库python发出发布请求

如何使用Python请求库发出发布请求?

尝试向它发出发布请求时未找到 Node app.get,但可以在浏览器中使用

在JAX-RS中发出发布请求时的最佳实践