2017-08-04 105 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

相关问题