Poonkodi Sivapragasam
Poonkodi Sivapragasam

Reputation: 1317

Unable to convert JSON object to Java object in Spring boot controller

I am giving the below input JSON Format to the spring controller. And I am getting an error while converting JSON Object to java in spring boot controller. All the details are given below.

{
      "series": "COR",
      "vehicleCondition": "N",
      "selectedOptions": {
        "snowplowBizUse": "N",
        "snowplowPersonalUse": "Y",
        "customSuspensionPkg": "Y"
      }
}

Given below is the input model for the spring controller,

package com.cnanational.productservice.model;

import java.util.List;

import javax.validation.constraints.Pattern;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class PsSeriesSelectedOptions {

    @Pattern(regexp = "\\p{Alnum}{1,30}")
    private String series;

    @Pattern(regexp = "\\p{Alnum}{1,30}")
    private String vehicleCondition;

    @JsonIgnoreProperties(ignoreUnknown = true)
    private List<selectedOptions> selectedOptions;

    public String getSeries() {
        return series;
    }

    public void setSeries(String series) {
        this.series = series;
    }

    public String getVehicleCondition() {
        return vehicleCondition;
    }

    public void setVehicleCondition(String vehicleCondition) {
        this.vehicleCondition = vehicleCondition;
    }

    public List<selectedOptions> getSelectedOptions() {
        return selectedOptions;
    }

    public void setSelectedOptions(List<selectedOptions> selectedOptions) {
        this.selectedOptions = selectedOptions;
    }

    public static class selectedOptions {

        private String snowplowBizUse;

        private String snowplowPersonalUse;

        private String customSuspensionPkg;

        public selectedOptions(
                String snowplowBizUse,
                String snowplowPersonalUse,
                String customSuspensionPkg) {
            this.snowplowBizUse = snowplowBizUse;
            this.snowplowPersonalUse = snowplowPersonalUse;
            this.customSuspensionPkg = customSuspensionPkg;
        }

        public String getSnowplowBizUse() {
            return snowplowBizUse;
        }

        public void setSnowplowBizUse(String snowplowBizUse) {
            this.snowplowBizUse = snowplowBizUse;
        }

        public String getSnowplowPersonalUse() {
            return snowplowPersonalUse;
        }

        public void setSnowplowPersonalUse(String snowplowPersonalUse) {
            this.snowplowPersonalUse = snowplowPersonalUse;
        }

        public String getCustomSuspensionPkg() {
            return customSuspensionPkg;
        }

        public void setCustomSuspensionPkg(String customSuspensionPkg) {
            this.customSuspensionPkg = customSuspensionPkg;
        }

    }

}

Given below is the method of controller,

@RequestMapping(value = PATH_GET_SURCHARGES, method=RequestMethod.POST)
    public ResponseEntity<?> getSurcharges(
            @RequestBody final PsSeriesSelectedOptions stdOptions)
    {
        log.debug("getSurcharges: entering, searchParams={}", stdOptions);

        System.out.println("one = "+stdOptions.getSeries());
        System.out.println("two = "+stdOptions.getVehicleCondition());

        System.out.println("six = "+stdOptions.getVehicleCondition());

        return this.productService.getSurcharges(
                stdOptions);
    }

}

Below is the error that i am getting while deploying.

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: java.io.PushbackInputStream@667e3422; line: 4, column: 26] (through reference chain: com.cnanational.productservice.model.PsSeriesSelectedOptions["selectedOptions"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
    at com.fasterxml.jackson.databind.DeserializationContext.reportMappingException(DeserializationContext.java:1234)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1122)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1075)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:338)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:269)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:259)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:26)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:504)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:104)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:357)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:148)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2922)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:241)
    ... 109 common frames omitted
2017-11-30 20:41:28:541 - WARN  - Correlation-Id = 1434383293 - AppName = product-service - Server-IP = 127.0.0.1:8100 - RequestorApp = UNKNOWN - RequestorIp = 127.0.0.1 - UserId = WEB_BKAUR - Total-Time = -1 - Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

Upvotes: 0

Views: 2721

Answers (1)

Sara M
Sara M

Reputation: 455

The problem is that your JSON does not match the type signature of your POJO. Jackson expects a JSON array to match Java List, Set, and Collection interface types. You'll need to update your JSON schema to:

{
    "series": "COR",
    "vehicleCondition": "N",
    "selectedOptions": [
        {
            "snowplowBizUse": "N",
            "snowplowPersonalUse": "Y",
            "customSuspensionPkg": "Y"
        }
    ]
}

This way Jackson will deserialize the selectionOptions property as a list of objects with the properties that are deserializable to the properties of your selectedOptions class. Here's an SO question going the other way that should help you understand what's going on here: How to use Jackson to deserialise an array of objects

Upvotes: 3

Related Questions