Search code examples
jsonspring-bootangular2-services

Posting a JSON Blob with Spring Boot and PostgreSQL


I am using Angular 2 to send the following JSON to a Spring Boot controller:

{
  "portal_name": "test",
  "app_name": "test",
  "app_owner": "test",
  "app_submitter": "test",
  "onboarding_form_blob": [
    {
      "newSplunkRow": "test"
    }
  ]
}

But when I send the JSON I get an this error from the Spring controller:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON document: Can not deserialize instance of java.lang.String out of START_ARRAY token

This is what the controller looks like for the Blob:

@Column(name = "onboarding_form_blob")
private String onboarding_form_blob;

And these are the Getters and Setters:

public String getOnboarding_form_blob() {
            return onboarding_form_blob;
        }

public void setOnboarding_form_blob(String onboarding_form_blob) {
            this.onboarding_form_blob = onboarding_form_blob;
        }

Solution

  • Your JSON payload is clear to me: JSON payload

    Not completely sure what your controller looks like. It'll be helpful if you provide the complete signature of the method in the controller and the complete Blob class. But I'm now guessing it looks something like this?

    My best guess

    If so: Then @Veeram is right. The issue is that the getters and setters for newSplunkRow expect a String, and you're sending an Array with one object instead of that String.

    You can fix it by either changing the controller or by changing the JSON you're sending to match the controller. What's best depends on the data you're trying to send and what you want to do with it once received. (From your @Column annotation I guess you're going to store it in the database? What do your tables look like?)