java spring项目controller中接收一个string,和一个List为入参报错

只接收一个List作为入参的话不报错,

下方为代码:

前台:

$.ajax({


    type:"post",

    url: 'add',

    contentType:"application/json; charset=utf-8",

    dataType:"json",

    data:JSON.stringify([{id:1,name:"hello"},{id:2,name:"hello"}]),

    success:function(data){

        console.log(data);

    }

})

后台:

@ResponseBody


@RequestMapping(value = "add", consumes = "application/json; charset=utf-8")

public String add(@RequestBody List<Tag> param) {

    System.out.println("param:" + param);

    return "成功";

}

前台控制台显示的数据格式为:

[{id: 1, name: "hello"}, {id: 2, name: "hello"}]

0:{id: 1, name: "hello"}

1:{id: 2, name: "hello"}


那如果我后台想接受一个字符串和一个list该怎么写呢,


后台代码改为:

@ResponseBody


@RequestMapping(value = "add", consumes = "application/json; charset=utf-8")

public String add(@RequestBody String content, @RequestBody List<Tag> param) {


前台应该怎么写?


炎炎设计
浏览 996回答 1
1回答

杨__羊羊

两种解决方案1、如果参数比较少 直接url?parm=value,那么就用@RequestParam注解2、封装成对象&nbsp; &nbsp; class A {&nbsp; &nbsp; &nbsp; &nbsp; private String parm1;&nbsp; &nbsp; &nbsp; &nbsp; private String parm2;&nbsp; &nbsp; &nbsp; &nbsp; private List<B> list;&nbsp; &nbsp; }对应的前台var data =new Object;data.param1="value";data.param2="value";var&nbsp; list =&nbsp; new Array();....省略list的数据初始化data.list = list;ajax的data&nbsp;data:JSON.stringify(data)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript