0

我正在尝试将此 JSON 传递给 Springs 控制器:

{"orderList":["IS_cb-A1-3","IS_cb-A1-4","IS_cb-A1-1","IS_cb-A1-2","IS_cb-A1-3"]}

这是方法声明:

public @ResponseBody JsonResponse orderCertificates(@ModelAttribute CertificateRequest certificateOrder, BindingResult result )

这是豆子:

public class CertificateRequest {
private String[] orderList = null;
private List<String> mbsResponse = new ArrayList<String>(); 

public String[] getOrderList() {
    return orderList;
}

public void setOrderList(String[] orderList) {
    this.orderList = orderList;
}

public List<String> getMbsResponse() {
    return mbsResponse;
}

public void setMbsResponse(List<String> mbsResponse) {
    this.mbsResponse = mbsResponse;
}
}

我不知道如何构建这个 bean(它不起作用)以便我可以访问这个数组。任何帮助,将不胜感激。

4

1 回答 1

2

This signature of the Request mapped method(in 3.1 Spring MVC) should work for you:

@RequestMapping(value="/cert", consumes="application/json", produces="application/json")
public @ResponseBody CertificateRequest orderCertificates(@RequestBody CertificateRequest certificateOrder){
//
}

I have tagged CertificateRequest with @RequestBody and removed BindingResult.

Update: Posting code, this is how my method looks -

@RequestMapping(value="/cert", consumes="application/json", produces="application/json")
public @ResponseBody CertificateRequest orderCertificates(@RequestBody CertificateRequest certificateOrder){
    System.out.println(certificateOrder);
    return certificateOrder;
}

and in the http request, I have explicitly set the Content-Type to "application/json"

于 2012-05-31T14:49:23.637 回答