【发布时间】:2021-05-28 16:00:01
【问题描述】:
我正在开发一个 Springboot 项目并从 Rest API 获取数据。响应是 XML 格式,我在将其转换为 JSON 时遇到了困难
XML 响应:
<StoreInfo>
<Store Number="1" NCPDPID="0411"/>
<Store Number="3" NCPDPID="1132"/>
<Store Number="4" NCPDPID="0407"/>
</StoreInfo>
我创建的 JSON 类是:
public class IDResponse {
private List<IDInfo> StoreInfo;
}
public class IDInfo {
private List<Store> Store;
}
public class Store {
private Integer Number;
private String ID;
}
获取和转换的代码:
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
ResponseEntity<String> response = restTemplate.exchange(requestUrl,
HttpMethod.GET, entity,
String.class);
XmlMapper xmlMapper = new XmlMapper();
responseData = xmlMapper.readValue(response, IDInfo.class);
我得到的异常:
- 方法抛出 'com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException' 例外。
- 无法识别的字段“Store”(类 com.walmart.datamodel.location.IDResponse),未标记为可忽略
如何解决这个问题?
【问题讨论】:
-
xmlMapper.readValue(response, IDInfo.class);?不应该是IDResponse -
@Turo,我试过了,但没用
标签: json xml spring-boot jackson