程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了通过POST从React App发送数组到Spring Boot大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决通过POST从React App发送数组到Spring Boot?

开发过程中遇到通过POST从React App发送数组到Spring Boot的问题如何解决?下面主要结合日常开发的经验,给出你关于通过POST从React App发送数组到Spring Boot的解决方法建议,希望对你解决通过POST从React App发送数组到Spring Boot有所启发或帮助;

最后,我解决了将所有参数包装在一个对象中的问题。

@JsonautoDetect
public class Params {

    privatE int[][] matrix;
    privatE int row;
    privatE int col;
    private int num;

    [...getters and setters]

然后在控制器的方法符号中仅声明一个参数:

    @PostMapPing(path = "/check")
    @CrossOrigin(origins = "http://localhost:3000")
    public Boolean check(@requestbody final Params params) {
        return sudokuGenerator.checkValue(params.getMatrix(), params.getRow(), params.getCol(), params.getNum());
    }

至关重要的是,客户端应该传递带有其属性的对象,而没有任何类型的包装,因此采用这种方式:

axios.post('http://localhost:8090/API/check', {
     matrix: this.props.rows,
     "row": row - 1,
     "col": col - 1,
     "num": input.textContent
})

而不是以这种方式(带有根属性“ params”):

axios.post('http://localhost:8090/API/check', {
     "params" : {
         matrix: this.props.rows,
         "row": row - 1,
         "col": col - 1,
         "num": input.textContent
     }
})

解决方法

我已经看到类似的问题,但没有成功。我需要将数字矩阵从Web应用程序(ReactJS)发送到Spring Boot控制器。

我尝试了很多组合,但总是会出错,我的有效载荷是:

{"rows":[[7,6,4,0],[9,8,[0,2,5,9,7,3,[4,1,4],5],[3,2],0]]}

我的反应代码是:

axios.post('http://localhost:8090/api/check',{
        rows: this.props.rows
    })
        .then(function (responsE) {
            console.log(responsE);
        })
        .catch(function (error) {
            console.log(error);
        });

我的Spring Boot控制器是:

@PostMapping(path = "/check")
@CrossOrigin(origins = "http://localhost:3000")
public Boolean check(@requestParam(value = "rows") final int[] array,final int row,final int col,final int num) {
    return true;
}

我已经尝试声明@requestParam(value = "rows[]")@requestParam(value = "rows")。而不是@requestParam(value = "rows") final Object rows

但是它总是以 错误400(错误请求) 响应。

如何通过POST请求传递矩阵?

谢谢

大佬总结

以上是大佬教程为你收集整理的通过POST从React App发送数组到Spring Boot全部内容,希望文章能够帮你解决通过POST从React App发送数组到Spring Boot所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。