Search code examples
javajacksonjackson-dataformat-csv

Jackson dataformat csv NoSuchMethodError createContentReference


I'm using jackson-dataformat-csv 2.15.0 with following code to export my data into an text file

            List<HashMap<String, String>> data = <list of data>;
            PrintWriter out = new PrintWriter(new File(FILE_PATH));
            CsvSchema schema = null;
            CsvSchema.Builder schemaBuilder = CsvSchema.builder();
            // output verify list record
            if (data != null && !data.isEmpty()) {
                schema = schemaBuilder.addColumn("ID").addColumn("Date").build()
                        .withLineSeparator("\r").withHeader();

                CsvMapper mapper = new CsvMapper();
                mapper.writer(schema).writeValues(out).writeAll(data);
            }
            out.close();

But it just pop up error

2023-05-14 14:11:25,865 ERROR [stderr] (Thread-394) Exception in thread "Thread-394" java.lang.NoSuchMethodError: com.fasterxml.jackson.dataformat.csv.CsvFactory._createContentReference(Ljava/lang/Object;)Lcom/fasterxml/jackson/core/io/ContentReference;

2023-05-14 14:11:25,865 ERROR [stderr] (Thread-394)     at com.fasterxml.jackson.dataformat.csv.CsvFactory.createGenerator(CsvFactory.java:372)

2023-05-14 14:11:25,865 ERROR [stderr] (Thread-394)     at com.fasterxml.jackson.dataformat.csv.CsvFactory.createGenerator(CsvFactory.java:16)

2023-05-14 14:11:25,865 ERROR [stderr] (Thread-394)     at com.fasterxml.jackson.databind.ObjectWriter.createGenerator(ObjectWriter.java:703)

2023-05-14 14:11:25,865 ERROR [stderr] (Thread-394)     at com.fasterxml.jackson.databind.ObjectWriter.writeValues(ObjectWriter.java:789)

I have checked maven hierarchy, those conflicted jackson libs are ommited. I also checked the exported WAR that its jackson-core, jackson-annotations, jackson-databind, and jackson-dataformat-csv are 2.15.0 and no other old-duplicated lib. No error reported from my eclipse about method not found or otherwise. Does anyone know what happen about this?


Solution

  • With further digging and some tips from the jackson lib's member, it turn out that isn't my WAR's fault but JBoss. Their default setting would use a module call resteasy-jackson2-provider and it will be using jackson-core and jackson-databind as part of its dependencies even if your server config doesn't includes them. It's indirect dependency.

    There are two ways to resolve this issue, I prefer first one for less trouble.

    1. Exclude them in your jboss-deployment-structure.xml

      As I mentioned that this is indirect dependency. So the lib that you have to exclude would be resteasy-jackson2-provider. Add them all to exclusions if you're not sure that the other JBoss would include them or not.

      <exclusions>
          <module name="com.fasterxml.jackson.core.jackson-core" />
          <module name="com.fasterxml.jackson.core.jackson-databind" />
          <module name="org.jboss.resteasy.resteasy-jackson2-provider" />
      </exclusions>
      
    2. Update JBoss's modules

      Well, this WILL be super annoying if you have many of them.