簡體   English   中英

引起:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:無法識別的字段“Status”

[英]Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “Status”

我收到以下錯誤消息,我有Status類但它沒有被識別。 我不知道如何繼續,也無法在線找到答案。

錯誤

   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;

我沒能重建你的問題。

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

我將依賴關系添加到jackson-dataformat-xml和woodstox-core-asl作為你的Stax實現(在我的測試項目中我使用的是Jackson 2.6.6。,Spring 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-boot示例,可以在這里轉換為Simple Spring-MVC

@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;
    ...
}

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

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

@XmlElement注釋位於Maven的jackson-dataformat-xml包中 - 該版本應與您的其他Jackson包版本匹配。)

另一種方法是將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配置的一部分包含在內,則此答案中有更多詳細信息。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM