1

I have a project where we use jackson to generate json, but when I use @JsonIgnore in the ejb package this is ignored in the web package. I that I could try to package jackson in the ear package and add <scope>provided</scope> to the other packages, but now I get this error java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonIgnore.

The url where i read this is Wildfly and Jackson @JsonIgnore annotation.

Url to my bitbucket containing an example project https://bitbucket.org/brimirnl/testing.

I'm using liberty v20.

This is my ear pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                         http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>nl.testing</groupId>
        <artifactId>test</artifactId>
        <version>1.0</version>
    </parent>

    <artifactId>ear-module</artifactId>
    <packaging>ear</packaging>
    <name>EAR module</name>
    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>ejb-module</artifactId>
            <type>ejb</type>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>war-module</artifactId>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.11.2</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.10.1</version>
                <configuration>
                    <modules>
                        <ejbModule>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>ejb-module</artifactId>
                        </ejbModule>
                        <webModule>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>war-module</artifactId>
                            <contextRoot>/foo</contextRoot>
                        </webModule>
                        <jarModule>
                            <groupId>com.fasterxml.jackson.core</groupId>
                            <artifactId>jackson-annotations</artifactId>
                            <version>2.11.2</version>
                            <includeInApplicationXml>true</includeInApplicationXml>
                        </jarModule>
                    </modules>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

This my ejb pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                         http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>nl.testing</groupId>
        <artifactId>test</artifactId>
      <version>1.0</version>
    </parent>
    <artifactId>ejb-module</artifactId>
    <packaging>ejb</packaging>
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.11.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>javax.ejb-api</artifactId>
            <version>3.2.2</version>
        </dependency>
    </dependencies>
</project>

And this is my war pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                         http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>nl.testing</groupId>
        <artifactId>test</artifactId>
        <version>1.0</version>
    </parent>

    <artifactId>war-module</artifactId>
    <packaging>war</packaging>
    <name>simple-webapp Maven Webapp</name>
    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>ejb-module</artifactId>
            <version>1.0</version>
            <type>ejb</type>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.ws</groupId>
            <artifactId>jaxws-api</artifactId>
            <version>2.3.1</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.11.2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <configuration>
                        <webXml>src\main\resources\WEB-INF\web.xml</webXml>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

This is my TestModel (it's in the ejb module)

package nl.testing;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class TestModel {
    private String name;
    @JsonIgnore
    private String description;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

This is de RS resource (it's in the war module)

package nl.testing;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/boe")
public class TestController {
    @GET
    public TestModel test() throws NoSuchFieldException {
        final TestModel model = new TestModel();
        model.setName("test");
        model.setDescription("desc");
        System.out.println(model.getClass().getDeclaredField("description").isAnnotationPresent(JsonIgnore.class));

        return model;
    }
}

If more information is required let me know and i'll add it :) Thanks in advance!

JvBarney
  • 41
  • 8
  • What exactly do you mean by "ignored in the web package"? – dpr Aug 10 '20 at 10:30
  • Sorry that's a good question! It should ignore that field, but it keeps coming up in the created json. I expect {"name":"test"} but I get {"name":"test","description":"desc"} – JvBarney Aug 10 '20 at 10:31
  • But the JSON in generated on the server side. Isn't it? The JSON string including description is transferred to the client and on the client it's de-serialized. – dpr Aug 10 '20 at 10:41
  • Yes, the JSON is generated on the server. – JvBarney Aug 10 '20 at 10:49
  • I added an url to my bitbucket with an example project – JvBarney Aug 10 '20 at 10:59
  • And your client code? – dpr Aug 10 '20 at 12:28
  • I dont have any client code. I just do a request with postman to `http://localhost:9080/foo/rest/boe` with an header `Accept: application/json` – JvBarney Aug 10 '20 at 13:56

0 Answers0