The code runs fine in the debugger and executes fine with Posman. However, with this addition I am getting the error
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:351) ~[gson-2.8.6.jar:na]
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80) ~[gson-2.8.6.jar:na]
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61) ~[gson-2.8.6.jar:na]
at com.google.gson.Gson.fromJson(Gson.java:932) ~[gson-2.8.6.jar:na]
at com.google.gson.Gson.fromJson(Gson.java:897) ~[gson-2.8.6.jar:na]
at com.google.gson.Gson.fromJson(Gson.java:846) ~[gson-2.8.6.jar:na]
at com.paperless.controler.PaperlessController.objects(PaperlessController.java:67) ~[classes/:na]
What I am trying to do is create the code for the client, but I am getting this error.
Your help will be greatly appreciated,
Alejandro Barrero
Spring has automatic JSON conversion for its MVC controllers. Just return the object that you want to serialize directly from the method.
On the client, I recommend you use a JAX-RS client implementation, such as the one Jersey offers. It also automatically (de)serializes objects if you plug in a JAX-B implemention such as Jackson.
P.S. there is no point to annotating your actions with @ResponseBody. @RestController already implies that all actions return a response body.
Thanks for the replies. I was trying plain JSON at the beginning but had difficulties. GSON constructs the string i want to return correctly; the problem is that I cannot construct the Java ArrayList or array, please tell me how to do it.
Your help will be greatly appreciated,
Alejandro Barrero