以下是我要传递的数据

var data=[{"TAG":"Taxes","REMARKS":"Enter Remarks"},{"TAG":"Taxes","REMARKS":"Enter Remarks"}];

Ajax调用将数据发布到控制器:

$.ajax({
    type: "POST",
    url: "rest/url",
    data: JSON.stringify(data),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) {
        console.log(result);
    },
    error : function(xml, status, xhr) {
        obj = (xml.responseText);
    }
});


@RequestMapping(value = "/url", method = RequestMethod.POST,consumes="application/json",produces="application/json")
public @ResponseBody
LinkedHashMap<String, ArrayList> getData(@RequestBody List<DataDTO> data,HttpServletRequest request1) {
System.out.println("Inside Controller :"+data.getClass())// getting Arraylist object
DataDTO dto1=data.get(0);// getting class cast exception


在上面的代码中,我能够以List格式获取控制器中的数据,但是当我将对象初始化为DTO类时,会得到ClassCastException

最佳答案

您的DataDTO类应该类似于以下内容,因为在AJAX中您使用的是TAGREMARKS。但是在DataDTO类中,它是TAGRemarks。因此,请按照以下步骤更改课程:

public class DataDTO {
    private String TAG;
    private String REMARKS;
    //Getters and setters
}

关于java - 通过AJAX调用将JSON对象列表传递给Controller,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58130672/

10-09 05:16