我正在使用Jackson ObjectMapper将String转换为Pojo。
我收到的消息:
{
"countryCode": "US",
"skuNumber": "TEST",
"itemTaxCd": "89999",
"lastModifiedBy": "xyz"
}
方法:
public void processImsProductTaxEvents(String event) {
try {
SkuMasterDTO message = objectMapper.readValue(event, SkuMasterDTO.class);
log.debug("Received message from IMS", message);
} catch (Exception e) {
log.error("Error in Object Mapper converting String to TaxCodeDTO", e);
}
}
现在countryCode位于SkuMasterDTO内的CountryDTO内,定义为Set
private Set<CountryDTO> countries = new HashSet<>();
CountryDTO课程
public class CountryDTO implements Serializable {
private static final long serialVersionUID = 1L;
@NotNull
@Size(max = 2)
private String countryCode;
我正在尝试将countryCode设置为集而不更改输入Json 。有什么建议吗?
答案 0 :(得分:3)
考虑在ObjectMapper
实例中使用DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAYS
。然后,您必须使用countryCode
将json字段countries
更改为Set countries
或将countryCode
标记为json属性@JsonProperty
,并在{{1}内提供构造函数/工厂方法参数。这将使您的对象映射器按照您的要求运行。
答案 1 :(得分:0)
您必须在SkuMasterDTO类中定义countryCode。
您可以添加一个getter并构建一个
Set<CountryDTO> getCountries(){
Set<CountryDTO> countries = new HashSet<>();
countries.add(new CountryDTO(countryCode));
return countries;
}
或者你可以准备set set set方法。