Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Convert JSON to Java

 
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following REST controller

Without

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.
 
Saloon Keeper
Posts: 12409
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop using GSON.

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.
 
Alejandro Barrero
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alejandro Barrero
Ranch Hand
Posts: 495
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies for taking your time. I don't have to convert the JSON  string; the following client code works fine
 
Bartender
Posts: 1955
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Alejandro,
I believe your application is a Spring boot application and I believe you have this dependency:


In this dependency, the JSON parsing is included.
The artifact that takes care of JSON parsing is:


In this artifact, there is another one:


Therefore, as long as you have the spring-boot-starter-web dependency, your JSON serialization / deserialization is handled.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic