Using json annotations with jackson and jax-rs on weblogic 12.2

1,867 views
Skip to first unread message

Claude Libois

unread,
Mar 28, 2017, 3:40:49 PM3/28/17
to jackson-user
Hello,
I have been fighting since yesterday to enforce the simple pojo to me marshalled as {rules: [...]}:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TRules", propOrder = {
    "rules"
})
@XmlRootElement(name = "Rules")
@JsonRootName("rules")
public class TRules {

    @XmlElement(name = "Rule", required = true)
    protected List<TRule> rules;
    public List<TRule> getRules() {
        if (rules == null) {
            rules = new ArrayList<TRule>();
        }
        return this.rules;
    }

}

However, I stil receive a json response with {Rule:[]}
I have tried to use jsonRootName annotation to change things but nothing is working.
By default weblogic is using moxy so I have enforced jackson usage by adding JacksonJsonProvider in my application class:
@ApplicationPath("/rest")

public class RulesApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(ProxyServiceImpl.class);
classes.add(TRules.class);
classes.add(TRule.class);
classes.add(RuleRS.class);
        classes.add(JacksonJsonProvider.class);
return classes;
}
}
I have tried also with JacksonFeatures/JacksonJaxbJsonProvider...
I have enforced weblogic to use my provided jackson library I have set up prefer-applicatio-packages for com.faster in the weblogic.xml
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <container-descriptor>
        <prefer-application-packages>
            <package-name>org.slf4j.*</package-name>
            <package-name>ch.qos.logback.*</package-name>
            <package-name>org.jboss.logging.*</package-name>
            <package-name>com.sun.xml.bind.*</package-name>
            <package-name>org.glassfish.jersey.media.*</package-name>
            <package-name>com.fasterxml.jackson.*</package-name>
        </prefer-application-packages>
    </container-descriptor>
    <context-root>ref-rules-front</context-root>    
</weblogic-web-app>
I have added a ton of maven dependencies because it wasn't clear for me which one should be used:
<dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.8.7</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-jaxb</artifactId>
            <version>2.25.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.7</version>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.jaxrs</groupId>
          <artifactId>jackson-jaxrs-json-provider</artifactId>
          <version>2.8.7</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-jaxrs</artifactId>
            <version>1.9.13</version>
        </dependency>



I'm totally lost because I think that whatever I do weblogic doesn't seems to use jackson. I have set some breakpoint in jackson code but no call.
Any help would be appreciated.
So the goal is just to have my list of rules name "rules" insteand of "Rule".
Best Regards,
Claude


Tatu Saloranta

unread,
Apr 3, 2017, 2:40:28 PM4/3/17
to jackson-user
I am not an expert with Weblogic, but I suspect there may be some
dependency that includes component that prevents use of Jackson. So...
Let's see: yes, you need jackson components:

- jackson-jaxrs-json-provider
- jackson-databind
- jackson-core (dependency of databind)
- jackson-annotations (dependency of databind)

of which last 2 may be brought as dependency anyway. For these version
2.8.7 makes sense.

> <dependency>
> <groupId>com.fasterxml.jackson.jaxrs</groupId>
> <artifactId>jackson-jaxrs-json-provider</artifactId>
> <version>2.8.7</version>
> </dependency>
>

This, I think, should not be included, and may bring "wrong stuff"

> <dependency>
> <groupId>org.glassfish.jersey.media</groupId>
> <artifactId>jersey-media-jaxb</artifactId>
> <version>2.25.1</version>
> <scope>test</scope>
> </dependency>
> <dependency>
> <groupId>com.fasterxml.jackson.core</groupId>
> <artifactId>jackson-annotations</artifactId>
> <version>2.8.7</version>
> </dependency>
> <dependency>
> <groupId>com.fasterxml.jackson.jaxrs</groupId>
> <artifactId>jackson-jaxrs-json-provider</artifactId>
> <version>2.8.7</version>
> </dependency>

Also remove this: it could use old (Jackson 1.x) version of things.
In fact, that may be the main problem here:

> <dependency>
> <groupId>org.codehaus.jackson</groupId>
> <artifactId>jackson-jaxrs</artifactId>
> <version>1.9.13</version>
> </dependency>
>
>
>
> I'm totally lost because I think that whatever I do weblogic doesn't seems
> to use jackson. I have set some breakpoint in jackson code but no call.
> Any help would be appreciated.
> So the goal is just to have my list of rules name "rules" insteand of
> "Rule".

One other thing: by default, Jackson does not add such wrapper, as
it's more a result of XML/JSON impedance (wrapper is needed in XML due
to different data model).
So while annotation `@JsonRootName` (or JAXB equivalent thereof) will
indicate what wrapper to use if need be, wrapping is not added unless

SerializationFeature.WRAP_ROOT_VALUE

is enabled. You may already be doing that, but if not, that'd be needed.
It may configured as default for `ObjectMapper`, or configured for
`ObjectWriter`.
I assume here former would be easier since JAX-RS provider controls
use of settings.

Hope this helps,

-+ Tatu +-



> Best Regards,
> Claude
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jackson-user...@googlegroups.com.
> To post to this group, send email to jackso...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages