我正在尝试将此JSON传递给Springs Controller:
{"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(这不起作用),以便我可以访问这个数组。任何帮助将不胜感激。
答案 0 :(得分:2)
Request map方法的签名(在3.1 Spring MVC中)应该对你有用:
@RequestMapping(value="/cert", consumes="application/json", produces="application/json")
public @ResponseBody CertificateRequest orderCertificates(@RequestBody CertificateRequest certificateOrder){
//
}
我已使用@RequestBody
标记了CertificateRequest并删除了BindingResult
。
更新:发布代码,这是我的方法看起来的方式 -
@RequestMapping(value="/cert", consumes="application/json", produces="application/json")
public @ResponseBody CertificateRequest orderCertificates(@RequestBody CertificateRequest certificateOrder){
System.out.println(certificateOrder);
return certificateOrder;
}
在http请求中,我已将Content-Type显式设置为“application / json”