由以下原因引起:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: unrecognized field "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"])

事件响应

@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 引导示例,可以转换为简单的 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 模块。

相关内容

最新更新