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

时间:2015-04-23 13:20:05

标签: java spring spring-mvc

我需要将一个请求和一个数组一起发送到Spring MVC Controller,并希望将它映射到Custom类的List:

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

和控制器类似:

@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,但它有相同的错误请求(需要参数数组但未提供)。

// subjectList就像:

[{
    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. 

1 个答案:

答案 0 :(得分:1)

您必须将#include<stdio.h> #include<stdlib.h> typedef struct Node{ int val; struct Node* next; } Node; void add_at_end(Node* head, int val){ Node* newNode = (Node *)malloc(sizeof(Node)); newNode -> next = NULL; if(head == NULL) { head = new_node; head->val = val; } if(head -> next == NULL) { new_node ->val = val; head -> next = newNode; } else{ Node *temp = head; while(temp -> next != NULL){ temp = temp -> next; } temp -> next = newNode; temp -> val = val; } } void display(Node* l){ while(l->next != NULL){ printf("%d -->", l->val); l = l->next; } printf("End\n"); } 替换为@RequestParam注释。

@RequestBody用于GET请求参数,而您提供的数据是POST请求正文。