java是由:com引起的。fasterxml。杰克逊。数据绑定。exc.UnrecognizedPropertyException:无法识别的字段“状态”
我收到以下错误消息,我有状态类,但未被识别。我不知道如何继续,也无法在网上找到答案
错误
org.springframework.http.converter.HttpMessageNotReadableException: Could
not read JSON: Unrecognized field "Status" (class
com.myproject.ticket.EventsResponse), not marked as ignorable (3 known
properties: "events", "status", "page"])
....
Caused by:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "Status" (class com.myproject.ticket.EventsResponse),
not marked as ignorable (3 known properties: "events", "status", "page"])
事件响应
@XmlRootElement(name = "EventsResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class EventsResponse {
@XmlElement(name = "Status")
private Status status;
@XmlElement(name = "Paging")
private Page page;
@XmlElementWrapper(name="Events")
@XmlElement(name = "Event")
private List<Event> events;
.....
状态
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Status {
@XmlElement(name = "Version")
private double version;
@XmlElement(name = "TimeStampUtc")
private Date timeStampUtc;
@XmlElement(name = "Code")
private int code;
@XmlElement(name = "Message")
private String message;
@XmlElement(name = "Details")
private String details;
响应
<EventsResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Status>
<Version>2.0</Version>
<TimeStampUtc>2016-06-11T09:32:21</TimeStampUtc>
<Code>0</Code>
<Message>Success</Message>
<Details />
</Status>
<Paging>
<PageNumber>1</PageNumber>
<PageSize>50</PageSize>
<PageResultCount>15</PageResultCount>
<TotalResultCount>15</TotalResultCount>
<TotalPageCount>1</TotalPageCount>
</Paging>
<Events>
<Event>
我将以下内容添加到状态,但仍收到相同的错误
@XmlElement(name = "Status")
@JacksonXmlProperty(localName = "Status")
private Status status;
# 1 楼答案
假设使用Jackson来反序列化XML对象,则有两种选择。最简单的方法是使用Jackson自己的XML注释,而不是或以及JAXB
@XmlElement
注释。例如:(Maven中的
@XmlElement
注释位于jackson-dataformat-xml
包中-版本应与其他Jackson包版本匹配。)另一种方法是注册一个AnnotationInvestor作为反序列化链的一部分,即(从单元测试中):
这可以识别
@XmlElement
注释。例如,如果需要将其作为Spring配置的一部分,那么this answer中有更多详细信息(为了使用
JaxbAnnotationIntrospector
类,需要Maven的jackson-module-jaxb-annotation
模块。)# 2 楼答案
我没能重建你的问题
我创建了一个测试项目github here,该项目具有满足您需求的Jackson配置和JAXB注释
我向jackson dataformat xml和woodstox core asl添加了依赖项,作为您的Stax实现(在我的测试项目中,我使用的是jackson 2.6.6.,Spring 4.2.6)
将Jackson2ObjectMapperBuilder配置为同时使用Jackson和JAXB注释。这是一个Spring引导示例,可以转换为简单的Spring MVC外观here
注意使用
它将省去您为首字母大写指定替代命名的需要,并且只需要在扭曲和重命名时使用JAXB注释,例如my Events Response将如下所示: