首页 > 解决方案 > HttpMessageNotReadableException:缺少所需的请求正文

问题描述

我有在 Spring 上运行的服务器,它有一个 rest 方法GET,它看起来像这样:

@GetMapping("/user")
    ParentInOut getUsersOnly(@RequestBody ParentInOut clientSide)
    {
        return new ParentInOut(userRepository.findAll());
    }

我正在尝试axios在我的 react.js 应用程序中使用来获取数据:

let dumbJSON={
        "messages":"",
        "localDateTime":getValue(parameter)
    };
    const config = {
        method: 'GET',
        url: url,
        body : dumbJSON,
        headers: {
            'Content-Type': 'application/json'
        }
    };
    axios(config).then(function (res) {
        console.log(res);
    }).catch(function (err) {
        console.log(err);
    });

但它给了我错误,说错误:请求失败,网络控制台中的状态码为 400

在 spring 控制台中,我得到了Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: warning

如何解决?

我也试过这个:

var headers = {
        'Content-Type': 'application/json'
    };
    axios
        .get(url, dumbJSON, {headers})
        .then(response => {
            console.log(response);
        })
        .catch(erro => {
            console.log(erro.response);
        });

给了我同样的错误和警告,但这次得到了一个响应,这是带有dumbJSON、headers、url等的axios对象本身。

标签: reactjsspringspring-bootgetaxios

解决方案


推荐阅读