简体   繁体   English

引起:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“Status”

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

I am receiving following error message, I have Status class but it is not being recognized. 我收到以下错误消息,我有Status类但它没有被识别。 I've got no idea how to proceed and could not find an answer online. 我不知道如何继续,也无法在线找到答案。

Error 错误

   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 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;

    .....

Status 状态

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

Response 响应

<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>

I added following to Status but I am still receiving the same error. 我添加了以下状态,但我仍然收到相同的错误。

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

I failed to reconstruct your issue. 我没能重建你的问题。

I created a test project github here that has Jackson configuration and JAXB annotations that meet your needs. 我在这里创建了一个测试项目github,它具有Jackson配置和JAXB注释,可满足您的需求。

I added dependencies to jackson-dataformat-xml and woodstox-core-asl as your Stax implementations (in my test project I am using Jackson 2.6.6. ,Spring 4.2.6) 我将依赖关系添加到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>

Configure the Jackson2ObjectMapperBuilder to use both Jackson and JAXB annotations. 配置Jackson2ObjectMapperBuilder以使用Jackson和JAXB注释。 This is a Spring-boot example to convert to Simple Spring-MVC look here 这是一个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;
 }
}

Note the use of 注意使用

PropertyNamingStrategy.PASCAL_CASE_TO_CAMEL_CASE

it will save you the need to specify alternative naming for first letter capitalization and will require the use for JAXB annotation only for warping and renaming for example my EventsResponse will look like: 它将为您节省为首字母大小写指定备用命名的需要,并且仅需要用于变形和重命名的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;
    ...
}

You have two options, assuming you are using Jackson to deserialize your XML objects. 假设您使用Jackson反序列化XML对象,则有两个选项。 The simplest is to use Jackson's own XML annotations instead of or as well as the JAXB @XmlElement annotations. 最简单的方法是使用Jackson自己的XML注释,而不是JAXB @XmlElement注释。 For example: 例如:

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

(The @XmlElement annotation is in the jackson-dataformat-xml package in Maven - the version should match your other Jackson package versions.) @XmlElement注释位于Maven的jackson-dataformat-xml包中 - 该版本应与您的其他Jackson包版本匹配。)

The alternative is to register an AnnotationIntrospector as part of your deserialization chain - ie. 另一种方法是将AnnotationIntrospector注册为反序列化链的一部分 - 即。 (from a unit test): (来自单元测试):

    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);

This recognises the @XmlElement annotation. 这可以识别@XmlElement注释。 There are more details in this answer if you need to include this as part of a Spring configuration, for example. 如果您需要将此作为Spring配置的一部分包含在内,则此答案中有更多详细信息。

(In order to use the JaxbAnnotationIntrospector class, you will need the jackson-module-jaxb-annotation module from Maven.) (为了使用JaxbAnnotationIntrospector类,您将需要Maven的jackson-module-jaxb-annotation模块。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段 - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field 解决com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段 - resolving com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“ g” - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “g” Jackson 反序列化错误:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别 - Jackson deserialization error: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException 无法使用杰克逊,com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException将xml绑定到pojo:无法识别的字段 - can not bind xml to pojo using jackson, com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field 对象映射器给出异常:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段 - Object Mapper giving Exception: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“消息”异常 - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “message” exception Java - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“”不可标记 - Java - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "" not marked as ignorable com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“user_activity” - com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "user_activity"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM