杰克逊以FAIL_ON_UNKNOWN_PROPERTIES失败

时间:2018-07-05 11:16:19

标签: jackson jersey

Jackson从Jersey客户端2.22迁移到具有Jackson Json提供程序的2.27,即使配置正确,Jackson也无法使用unknwown属性;

    final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider()
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Client client = ClientBuilder.newClient().register(jacksonJsonProvider);

...

    javax.ws.rs.core.Response r = requestBuiler.post(Entity.form(formData));
    return r.readEntity(AccessTokenResponse.class);

如果对象中不存在某些字段,则响应反序列化将崩溃/请参阅堆栈跟踪。

我不要在对象中添加任何注释。在以前的版本中不需要。

堆栈跟踪

  

原因:   com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:   无法识别的字段“ mobileNo”(类   ch.abraxas.oauth.egovsec.EGovUser),未标记为可忽略(已知20   属性:“ needsApproval”,“姓氏”,“ authType”,   “ accountNonExpired”,“ externalId”,“标识符”,“ accountNonLocked”,   “ otherRoles”,“ cariId”,“ credentialsNonExpired”,“用户名”,   “语言”,“组织”,“ contextName”,“ firstname”,“ email”,   [来源]:“机构”,“国家”,“座机号码”,“已启用”])   (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor $ UnCloseableInputStream);   行:1,列:1933](通过参考链:   ch.abraxas.oauth.egovsec.client.AccessTokenResponse [“ user”]-> ch.abraxas.oauth.egovsec.EGovUser [“ mobileNo”]))      在   com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:62)      在   com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:834)   ...

pom.xml

       <javaee.version>8.0</javaee.version>
       <jersey.version>2.27</jersey.version>
       <jackson.version>2.9.4</jackson.version>

       <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <scope>provided</scope>
    </dependency>

        <!-- Jersey stack -->
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
            <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>${jackson.version}</version>
    </dependency>

当addind缺少属性时,它工作正常。但这应该支持缺少属性。

谢谢您的帮助!

0 个答案:

没有答案