JBoss resteasy - 自定义 Jackson 提供程序 [英] JBoss resteasy - Custom Jackson provider

查看:21
本文介绍了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",因此默认提供程序优先,因此将application/json"添加到自定义 JsonProvider 解决了该问题.

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天全站免登陆