2016-06-09 72 views
5

我收到以下錯誤消息,我有狀態類但它不被識別。我不知道如何繼續,也無法在網上找到答案。引起:com.fasterxml.jackson.databind.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"]) 

EventsResponse

@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

您是否有XML示例? –

+0

@MattPearce是包含在問題中。 –

+0

可以提供整個堆棧跟蹤 –

回答

1

假設您使用Jackson反序列化XML對象,您有兩種選擇。最簡單的方法是使用Jackson自己的XML註釋,而不是JAXB @XmlElement註釋。例如:

@XmlElement(name = "Status") 
@JacksonXmlProperty(localName = "Status") 
private Status status; 

(該@XmlElement註釋是在jackson-dataformat-xml包Maven中 - 的版本應該與其他傑克遜包版本)

另一種方法是註冊一個AnnotationIntrospector作爲反序列化鏈的一部分 - 即。 (來自單元測試):

XmlMapper mapper = new XmlMapper(); 
    AnnotationIntrospector aiJaxb = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()); 
    mapper.setAnnotationIntrospector(aiJaxb); 
    // EVENTS_RESPONSE is the incoming XML 
    EventsResponse response = mapper.readValue(EVENTS_RESPONSE, EventsResponse.class); 

這確認了@XmlElement註釋。例如,如果您需要將其作爲Spring配置的一部分包含在內,則可以在this answer中找到更多詳細信息。

(爲了使用JaxbAnnotationIntrospector類,您將需要的Maven的jackson-module-jaxb-annotation模塊。)

+0

謝謝,我添加了您建議的註釋,但似乎我正在使用不同的依賴性來使用您的內容。由於IDE說jacksonXmlProperty無法解析爲類型。爲什麼我的代碼識別狀態?爲什麼我應該使用JacksonXmlProperty? –

+0

我已經添加了一些關於所需依賴關係的註釋。 Jackson 2.x不支持默認的JAXB註釋 - 請參閱其wiki頁面(http://wiki.fasterxml.com/JacksonJAXBAnnotations)以獲取更多詳細信息。 –

+0

我跟着你的第一個解決方案,但代碼仍然返回以下錯誤。 org.springframework.http.converter.HttpMessageNotReadableException:無法讀取JSON:無法識別的字段「Status」(類com.myproject.ticket.EventsResponse),未標記爲可忽略(3個已知屬性:「events」,「status」,「page 「])問題已更新。 –

3

我沒能重建您的問題。

我創建了一個測試項目github here,它具有滿足您需求的Jackson配置和JAXB註釋。

我添加依賴於傑克遜DATAFORMAT-xml和woodstox核-ASL爲您的StAX實現(在我的測試項目中,我使用傑克遜2.6.6,4.2.6春季)

<dependency> 
    <groupId>com.fasterxml.jackson.dataformat</groupId> 
    <artifactId>jackson-dataformat-xml</artifactId> 
    <version>2.6.6</version> 
</dependency> 
<dependency> 
    <groupId>org.codehaus.woodstox</groupId> 
    <artifactId>woodstox-core-asl</artifactId> 
    <version>4.4.1</version> 
</dependency> 

配置Jackson2ObjectMapperBuilder同時使用Jackson和JAXB註釋。這是一個Spring引導例如轉換成簡單的彈簧-MVC看here

@SpringBootApplication 
public class EventAppConfiguration { 

    public static void main(String[] args) { 
     SpringApplication.run(EventAppConfiguration.class, args); 
    } 

    @Bean 
    public Jackson2ObjectMapperBuilder jacksonBuilder() { 
     Jackson2ObjectMapperBuilder b = new Jackson2ObjectMapperBuilder(); 
     b.indentOutput(true) 
     //Enable Introspects for both Jackson and JAXB annotation 
    .annotationIntrospector(introspector()) 
     //Use CamelCase naming 
    .propertyNamingStrategy(PropertyNamingStrategy.PASCAL_CASE_TO_CAMEL_CASE) 
    .dateFormat(new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss")); 
     return b; 
    } 
    @Bean 
    public AnnotationIntrospector introspector(){ 
    AnnotationIntrospector primary = new JacksonAnnotationIntrospector(); 
    AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()); 
    AnnotationIntrospector pair = AnnotationIntrospector.pair(primary, secondary); 
    return pair; 
} 
} 

注意使用

PropertyNamingStrategy.PASCAL_CASE_TO_CAMEL_CASE 

這將節省您需要指定替代命名爲第一個字母大寫和意志只需要用於JAXB註釋僅用於翹曲和重命名,例如我的EventsResponse將如下所示:

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class EventsResponse { 
     private Status status; 
     @XmlElement(name = "Paging") 
     private Page page; 
     @XmlElementWrapper(name = "Events") 
     @XmlElement(name = "Event") 
     private List<Event> events; 
    ... 
}