JBoss resteasy-定制Jackson提供商 [英] JBoss resteasy - Custom Jackson provider

查看:117
本文介绍了JBoss resteasy-定制Jackson提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring引导应用程序中使用JBoss resteasy. 如下配置了我的自定义JasonProvider并使用com.fasterxml.jackson.

I am using JBoss resteasy in my Spring boot application. Have configured with my custom JasonProvider like below and using com.fasterxml.jackson.

    @Provider
    @Priority(value=1)
    @Consumes({ "application/*+json", "text/json" })
    @Produces({ "application/*+json", "text/json" })
    public class JsonProvider extends JacksonJsonProvider {

      public static final PeriodFormatter STANDARD_ISO_PERIOD_FORMAT = ISOPeriodFormat.standard();

        public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().configure(WRITE_DATES_AS_TIMESTAMPS, false).configure(FAIL_ON_UNKNOWN_PROPERTIES, false)
                    .setDateFormat(ISO8601_WITH_MILLIS);

    static {
            final SimpleModule module = new SimpleModule("JsonProviderModule", new Version(1, 0, 0, null, null, null));
            module.addSerializer(Date.class, new DateSerializer());
            module.addDeserializer(Date.class, new DateDeserializer());

            OBJECT_MAPPER.registerModule(module);
        }

   public JsonProvider() {
        super(OBJECT_MAPPER);
    }
}

在我的客户代码中,

    final Client client = factory.getClient();
    client.register(jsonProvider);

尽管jsonProvider已注册,但是当我打电话时,它没有调用我的jsonProvider.

Though the jsonProvider is registered, When i make a call, it is not invoking my jsonProvider.

起因: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: 无法识别的字段……位于 com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:26) 〜[jackson-databind-2.8.9.jar:2.8.9]在 com.fasterxml.jackson.databind.ObjectReader._bind(ObjectReader.java:1583) 〜[jackson-databind-2.8.9.jar:2.8.9]在 com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:964) 〜[jackson-databind-2.8.9.jar:2.8.9]在 org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider.readFrom(ResteasyJackson2Provider.java:134) 〜[resteasy-jackson2-provider-3.1.4.Final.jar:3.1.4.Final]在 org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext.readFrom(AbstractReaderInterceptorContext.java:66) 〜[resteasy-jaxrs-3.1.4.Final.jar:3.1.4.Final]在 org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:56) 〜[resteasy-jaxrs-3.1.4.Final.jar:3.1.4.Final]在 org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:266) 〜[resteasy-client-3.1.4.Final.jar:3.1.4.Final] ... 29个常见框架 省略

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field ...... at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:26) ~[jackson-databind-2.8.9.jar:2.8.9] at com.fasterxml.jackson.databind.ObjectReader._bind(ObjectReader.java:1583) ~[jackson-databind-2.8.9.jar:2.8.9] at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:964) ~[jackson-databind-2.8.9.jar:2.8.9] at org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider.readFrom(ResteasyJackson2Provider.java:134) ~[resteasy-jackson2-provider-3.1.4.Final.jar:3.1.4.Final] at org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext.readFrom(AbstractReaderInterceptorContext.java:66) ~[resteasy-jaxrs-3.1.4.Final.jar:3.1.4.Final] at org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:56) ~[resteasy-jaxrs-3.1.4.Final.jar:3.1.4.Final] at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:266) ~[resteasy-client-3.1.4.Final.jar:3.1.4.Final] ... 29 common frames omitted

它过去经常与resteasy 3.0.14.Final版本配合使用.

It used to work fine with the resteasy version 3.0.14.Final.

我最近升级到3.1.4.Final,并且还有其他几个Jars.不知道为什么不采用最新版本的自定义JsonProvider.

I recently upgraded to 3.1.4.Final and there are couple of other Jars also. Not sure why it is not taking the custom JsonProvider with the latest version.

还有其他注册方式吗?

相关pom整体

<dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson2-provider</artifactId>
            <version>3.1.4.Final</version>
        </dependency>

<dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-validator-provider-11</artifactId>
            <version>3.1.4.Final</version>
        </dependency>

还有其他pom冲突需要验证吗...

Is there any other pom conflict to be validated ...

谢谢

推荐答案

添加"application/json"以及其他注释解决了该问题.

Adding "application/json" along with the other annotation solved the issue.

@Provider
@Consumes({ "application/json","application/*+json", "text/json" })
@Produces({ "application/json","application/*+json", "text/json" })
public class JsonProvider extends JacksonJsonProvider {

Spring根据匹配项设置优先级&重量.由于默认JsonProvider在版本3.1.0中添加了注释"application/json",因此默认提供者具有优先权,因此向自定义JsonProvider添加"application/json"解决了该问题.

Spring sets priority based on the match & weightage. Since, the default JsonProvider added the annotation "application/json" in the version 3.1.0, default provider takes precedence and hence adding "application/json" to the custom JsonProvider fixed the issue.

这篇关于JBoss resteasy-定制Jackson提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆