2017-08-04 100 views
0

問題是我收到此錯誤。無法將SaleListDTO的實例反序列化爲START_ARRAY令牌

我必須模擬剩餘服務調用,因爲它現在正由另一個團隊開發。

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.egencia.service.invoiceaggregator.cache.SaleListDTO out of START_ARRAY token 

這裏是我的傑克遜映射豆

Jackson2ObjectMapperBuilder 
       .json() 
       .featuresToEnable(DeserializationFeature.UNWRAP_ROOT_VALUE, DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)         .serializationInclusion(JsonInclude.Include.NON_NULL) 
       .serializationInclusion(JsonInclude.Include.NON_EMPTY) 
       .failOnUnknownProperties(false) 
       .build(); 
@JsonRootName("list") 
public class SaleListDTO { 
    private SaleDTO[] list; 
    public SaleDTO[] getList() { 
     return list; 
    } 
    public void setList(SaleDTO[] list) { 
     this.list = list; 
    } 
} 

這裏是JSON文件

{"list": [ 
    { 
     "id": 111111, 
     "currency": "EUR", 
     "country": "ITA", 
     "name": "Italy", 
     "code": "IT" 
    },... 
]} 

我已經測試了這麼多的組合,但不成功。請幫忙

回答

1

刪除@JsonRootName(「list」)。

這裏是工作示例:

@Getter 
@Setter 
@Data 
@AllArgsConstructor 
@NoArgsConstructor 
@ToString 
public class SaleListDTO { 

    @JsonProperty("list") 
    private SaleDTO[] list; 



} 


@Getter 
@Setter 
@Data 
@AllArgsConstructor 
@NoArgsConstructor 
@ToString 
public class SaleDTO { 

    private int id; 
    private String currency; 
    private String country; 
    private String name; 
    private String code; 



} 

測試方法:

@Test 
    public void testConversion() throws JsonParseException, JsonMappingException, IOException{ 
     ObjectMapper mapper=new ObjectMapper(); 
     SaleListDTO dto=mapper.readValue(new File(PATH), SaleListDTO.class); 
     System.out.println(dto.toString()); 
    } 

響應:

SaleListDTO(list=[SaleDTO(id=111111, currency=EUR, country=ITA, name=Italy, code=IT), SaleDTO(id=22222, currency=IN, country=INDIA, name=CHENNAI, code=IT)]) 
+0

嗨@Barath,從哪個庫你服用的註解? – Sofiane

+0

這是從項目的龍目島 – Sofiane

+0

org.projectlombok 龍目島 18年1月16日 編譯 Sofiane

相關問題