gpt4 book ai didi

java - 尝试将 js 数组映射到 List 时出现错误请求,Spring MVC

转载 作者:行者123 更新时间:2023-12-01 11:37:28 25 4
gpt4 key购买 nike

我需要将请求和数组一起发送到 Spring MVC Controller ,并希望将其映射到自定义类列表中:

$.ajax({
url:contextPath+ "/auth/manage/getAuthList.do",
type: "POST",
data: {subjectList:subjectList}
.....
}

Controller 就像:

@RequestMapping(value = "/getAuthList.do",method = RequestMethod.POST)
@ResponseBody
public JsonResult getAuthorizedList(@RequestParam List<Subject> subjectList, HttpSession session) throws UnsupportedEncodingException, SQLException {

但是当发送请求时,我收到了错误的请求。

Required List parameter 'subjectList' is not present

我也尝试过使用 Subject[] 而不是 List 但它有同样的错误请求(需要参数数组但未提供)。

//主题列表如下:

[{
btId:9
btName:"tx3"
btNameCN:"YYY",
serviceId:1,
subjectId:9401,
suebjectNameCN:"XXX"
},
btId:9
btName:"tx3"
btNameCN:"YYY2",
serviceId:1,
subjectId:9402,
suebjectNameCN:"XXX2"
}
]

这是我的Subject

public class Subject implements Serializable{

private int subjectId;
private String subjectNameCN;
private int btId;
private String btName;
private String btNameCN;
private int serviceId;

public Subject(int subjectId, String subjectNameCN, int btId,
String btName, String btNameCN, int serviceId) {
super();
this.subjectId = subjectId;
this.subjectNameCN = subjectNameCN;
this.btId = btId;
this.btName = btName;
this.btNameCN = btNameCN;
this.serviceId = serviceId;
}


public int getServiceId() {
return serviceId;
}
public void setServiceId(int serviceId) {
this.serviceId = serviceId;
}

public int getSubjectId() {
return subjectId;
}
public void setSubjectId(int subjectId) {
this.subjectId = subjectId;
}

public String getSubjectNameCN() {
return subjectNameCN;
}
public void setSubjectNameCN(String subjectNameCN) {
this.subjectNameCN = subjectNameCN;
}

public int getBtId() {
return btId;
}
public void setBtId(int btId) {
this.btId = btId;
}

public String getBtName() {
return btName;
}
public void setBtName(String btName) {
this.btName = btName;
}

public String getBtNameCN() {
return btNameCN;
}
public void setBtNameCN(String btNameCN) {
this.btNameCN = btNameCN;
}

//please discard this two method since it is wrong getter/setter but legacy ones
public int getserviceId() {
return serviceId;
}
public void setserviceId(int serviceId) {
this.serviceId = serviceId;
}

}

更新:

尝试将@RequestParam替换为@RequestBody,得到415

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method. 

最佳答案

您必须将 @RequestParam 替换为 @RequestBody 注释。

@RequestParam 用于 GET 请求参数,而您提供数据作为 POST 请求正文。

关于java - 尝试将 js 数组映射到 List<CustomClass> 时出现错误请求,Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29824728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com