JBoss resteasy - Custom Jackson provider

2020-02-07 09:50发布

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

In my client code,

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

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

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

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

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.

Is there any other way to register?

Related pom entires,

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

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

Thanks

1条回答
ゆ 、 Hurt°
2楼-- · 2020-02-07 10:33

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

查看更多
登录 后发表回答