在使用一项服务时,我遇到了“无法从 [Source: (ByteArrayInputStream) 的 START_OBJECT 令牌中反序列化 java.util.ArrayList 实例”的错误。但是在添加此注释 @JsonFormat(with = JsonFormat.Feature. ACCEPT_SINGLE_VALUE_AS_ARRAY) 在 dto 类列表属性级别。
DTO类:
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2021-04-14T12:04:58.520-04:00")
public class OverseasPagedModelEntityModelOutlet
{
@JsonProperty("_embedded")
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
private List<OverseasEntityModelOutlet> embedded = null;
@JsonProperty("_links")
private OverseasLinks links = null;
@JsonProperty("page")
private OverseasPageMetadata page = null;
public OverseasPagedModelEntityModelOutlet embedded(final List<OverseasEntityModelOutlet> embedded)
{
this.embedded = embedded;
return this;
}
public OverseasPagedModelEntityModelOutlet addEmbeddedItem(final OverseasEntityModelOutlet embeddedItem)
{
if (this.embedded == null)
{
this.embedded = new ArrayList<OverseasEntityModelOutlet>();
}
this.embedded.add(embeddedItem);
return this;
}
}
我想将此 DTO 与响应对象映射,但在设置 @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY) 后,所有属性值都作为 Null 响应。但是在 POSTMAN 中,所有属性都填充了值。
这是服务请求和响应的详细信息。但是收到此错误
使用此请求参数在 POSTMAN 中进行了测试,并且工作正常
1.请求正文期待json如下:
{
"countryIsoCodes": [
"TH"
]
}
POSTMAN 中的响应
{ "_embedded": { "outlets": [ { "outletId": "GD0002369", "companyId": "GP0001502", "legalName": "XHBD Tic.TAŞ. Adana Şubesi - XBYU KBF", "registeredOffice": true ,“mainOutlet”:假,“附属”:假,“startOperationDate”:“2002-01-01”,“outletIsActive”:真,“businessNames”:[{“businessName”:“abcd Adana”,“brand”: { “id”:“MBD”,“_links”:{ “brand”:{} } } },{ “businessName”:“DSE Adana”,“brand”:{ “id”:“SMT”,“_links” : { "brand": {} } } } ], "address": { "street": "Ceyhan Yolu Üzeri 5.km Polis Okulu Karşısı Yüreğir", "zipCode": "01310", "city": "Adana", "country": { "isoCode": "TR", "_links": { "country": { "href": “https://abcd-int.i.xyz.com/int/abcd-api/api/v1/countries/TR”}}}}}}]}
}
这在邮递员中按预期工作。
我想用上面提到的我的 DTO OverseasPagedModelEntityModelOutlet.java 映射这个响应。它是使用 ParameterizedTypeReference 映射的
final ParameterizedTypeReference<OverseasPagedModelEntityModelOutlet> returnType = new ParameterizedTypeReference<OverseasPagedModelEntityModelOutlet>()
{
};
并通过传递 requestBody 和 returnType 调用 API,如下所示
final ResponseEntity<T> responseEntity = restTemplate.exchange(requestEntity, returnType);
但是 responseEntity 将所有对象和属性值显示为 NULL。这是 responseEntity :
OverseasPagedModelEntityModelOutlet {
embedded: [class OverseasEntityModelOutlet {
links: null
additionalAddress: null
address: null
affiliate: null
brandCodes: null
businessNames: null
closeDownDate: null
closeDownReason: null
companyId: null
distributionLevels: null
geoCoordinates: null
gssnClassicId: null
legalName: null
mainOutlet: null
nameAddition: null
outletId: null
outletIsActive: null
overallUpdateTimestamp: null
postOfficeBox: null
predecessors: null
region: null
registeredOffice: null
startOperationDate: null
successors: null
translations: null
updateTimestamp: null
}]
links: class OverseasLinks {
empty: null
}
page: class OverseasPageMetadata {
number: 1
size: 10
totalElements: 109
totalPages: 11
}
}
但实际上,如果您通过 POSTMAN 调用此 API,所有值都会按预期反映。但不确定为什么 null 值会通过代码出现。
提前感谢您的帮助。