0

I am trying to pass this JSON to a Springs Controller:

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

Here is the method declaration:

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

And here is the bean:

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;
}
}

I don't know how to build this bean (which doesn't work) so that I can get to this array. Any help would be appreciated.

1 Answer 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"

Sign up to request clarification or add additional context in comments.

2 Comments

When orderList is a String: no problem. It needs to be an Array, which is when it stops working.
Yes, it works as an array also, in my machine this is the string printed in the method body:CertificateRequest [orderList=[IS_cb-A1-3, IS_cb-A1-4, IS_cb-A1-1, IS_cb-A1-2, IS_cb-A1-3], mbsResponse=[]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.