webcompere / model-assert

Assertions for data models
MIT License
28 stars 3 forks source link

Including ModelAssert in POM causes 'Failed to instantiate SLF4J LoggerFactory' #38

Closed iammac2 closed 3 years ago

iammac2 commented 3 years ago

Including the POM dependency:

        <!-- https://mvnrepository.com/artifact/uk.org.webcompere/ModelAssert -->
        <dependency>
            <groupId>uk.org.webcompere</groupId>
            <artifactId>ModelAssert</artifactId>
            <version>1.0.0</version>
        </dependency>

Causes the following error:

Failed to instantiate SLF4J LoggerFactory
Reported exception:
java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonMerge
    at com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector.<clinit>(JacksonAnnotationIntrospector.java:50)
    at com.fasterxml.jackson.databind.ObjectMapper.<clinit>(ObjectMapper.java:357)
    at net.logstash.logback.composite.CompositeJsonFormatter.createJsonFactory(CompositeJsonFormatter.java:123)
    at net.logstash.logback.composite.CompositeJsonFormatter.start(CompositeJsonFormatter.java:104)
    at net.logstash.logback.LogstashFormatter.start(LogstashFormatter.java:154)
    at net.logstash.logback.encoder.CompositeJsonEncoder.start(CompositeJsonEncoder.java:211)
    at ch.qos.logback.core.joran.action.NestedComplexPropertyIA.end(NestedComplexPropertyIA.java:161)
    at ch.qos.logback.core.joran.spi.Interpreter.callEndAction(Interpreter.java:309)
    at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:193)
    at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:179)
    at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:62)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53)
    at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:75)
    at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:150)
    at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:84)
    at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:55)
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
    at uk.gov.ho.domain.component.api.services.testControl.TestControlService.<clinit>(TestControlService.java:11)

Where our TestControlService has the following:

import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.gov.ho.domain.component.api.constant.testControl.TestControlEndpoint;
import uk.gov.ho.domain.component.api.dto.ipic.testControl.*;

public class TestControlService  {

    private static final Logger LOGGER = LoggerFactory.getLogger(TestControlService.class);
org/slf4j/slf4j-api/1.7.26/slf4j-api-1.7.26.jar!/org/slf4j
ashleyfrieze commented 3 years ago

@iammac2 Looks like the issue is in your logstash agent, rather than in sl4j itself. I imagine there are two possibilities here. One is that logstash is using an older Jackson. ModelAssert uses an up to date one to avoid security scanners complaining.

Another possibility is that logstash does some class path scanning to see if you have some of Jackson, and then assumes you have all of it. This may be as simple as just adding the https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations/2.12.3 dependency into your project (you can mark it as test scope) so that the problem is solved.

iammac2 commented 3 years ago

Will try the approaches recommended. The test framework is common to many teams so it may not be easy to change the Jackson version.

ashleyfrieze commented 3 years ago

@iammac2 my guess is that you just need to add jackson-annotations to your build and it'll work. In terms of using an older version, you can excluse jackson-databind from being imported transitively by ModelAssert and import your own version. There are no special features of jackson being used by ModelAssert so it should work on an older jar.

ashleyfrieze commented 3 years ago

@iammac2 - any luck with this?

iammac2 commented 3 years ago

Not yet no. Not had the time yet, sorry. Please close the issue if you need to as it's isolated to the environment I'm on and shouldn't affect others.

ashleyfrieze commented 3 years ago

@iammac2 - happy to advise you further if you get any other issues getting it working in your environment, but yeah, I don't think this relates to the model assert library itself.