User Tools

Site Tools


java:restlet:jackson

Using jackson in Restlet (jaxrs) is quite simple but has some pitfalls.

I was using following constellations:

Maven dependencies

        <dependency>
            <groupId>org.restlet.jse</groupId>
            <artifactId>org.restlet</artifactId>
            <version>3.0-M1</version>
        </dependency>
        <dependency>
            <groupId>org.restlet.jse</groupId>
            <artifactId>org.restlet.ext.json</artifactId>
            <version>3.0-M1</version>
        </dependency>
        <dependency>
            <groupId>org.restlet.jse</groupId>
            <artifactId>org.restlet.ext.jaxrs</artifactId>
            <version>3.0-M1</version>
            <exclusions>
                <exclusion>
                    <artifactId>stax-api</artifactId>
                    <groupId>javax.xml.stream</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- Must be before org.restlet.ext.jackson!!!! -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.6</version>
        </dependency>
        <dependency>
            <groupId>org.restlet.jse</groupId>
            <artifactId>org.restlet.ext.jackson</artifactId>
            <version>3.0-M1</version>
            <exclusions>
                <exclusion>
                    <artifactId>stax-api</artifactId>
                    <groupId>javax.xml.stream</groupId>
                </exclusion>
            </exclusions>
        </dependency>

Service

    @Path("rest/inout")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public MyOutputType inout(MyInputType input) {
        // Do some Stuff
        return result;
    }

MyInput/OutputType

It is very important to use bean classes to get default BeanSerializer of jackson.

module-info.java

    requires org.restlet.ext.jackson;
    exports info.elsener.package to com.fasterxml.jackson.databind;
    exports info.elsener.package to org.restlet.ext.jaxrs;

requires

The line 'requires org.restlet.ext.jackson;' is needed to provide the 'JacksonConverter' to your project. Otherwise it will not be automatic recognized by restlet.

exports

The lines 'exports ….' will allow jaxrs and jackson to read and write your Input/Output-Types.

java/restlet/jackson.txt · Last modified: 2018/08/01 09:52 by els