Search code examples
spring-bootgraphqlgraphql-java

Sring Boot 1.5.20 / GraphQl java extended scalar for 'Json' support gives java.lang.NoClassDefFoundError during application startup


I am trying to integrate graphql using spring boot 1.5.20. I have included the extended-scalars library to provide support for 'JSON' type.

<dependency>
    <groupId>com.graphql-java</groupId>
    <artifactId>graphql-spring-boot-starter</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>com.graphql-java</groupId>
    <artifactId>graphql-java-tools</artifactId>
    <version>4.3.0</version>
</dependency>
<dependency>
    <groupId>com.graphql-java</groupId>
    <artifactId>graphql-java-extended-scalars</artifactId>
    <version>2018-10-05T23-44-12-5312ccd</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

I need to make use of the scalar type json. For this, I have defined a @Bean annotated method to configure extended scalar.

@Bean
public GraphQLScalarType jsonType() {
    return ExtendedScalars.Json;
}

I have also included 'scalar JSON' in the schema file.

When I add @Bean annotated method to configure extended scalar for JSON, I get 'Factory method 'jsonType' threw exception; nested exception is java.lang.NoClassDefFoundError: graphql/schema/CoercingParseLiteralException' error during application startup

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jsonType' defined in class path resource [.../configuration/GraphQLConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [graphql.schema.GraphQLScalarType]: Factory method 'jsonType' threw exception; nested exception is java.lang.NoClassDefFoundError: graphql/schema/CoercingParseLiteralException
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1177)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1072)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1317)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1283)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveMultipleBeans(DefaultListableBeanFactory.java:1161)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1097)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1067)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583)
    ... 77 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [graphql.schema.GraphQLScalarType]: Factory method 'jsonType' threw exception; nested exception is java.lang.NoClassDefFoundError: graphql/schema/CoercingParseLiteralException
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 92 common frames omitted
Caused by: java.lang.NoClassDefFoundError: graphql/schema/CoercingParseLiteralException
    at graphql.scalars.datetime.DateTimeScalar.<init>(DateTimeScalar.java:27)
    at graphql.scalars.ExtendedScalars.<clinit>(ExtendedScalars.java:40)
    at com.logmein.assets.orchestrationservice.configuration.GraphQLConfiguration.jsonType(GraphQLConfiguration.java:29)
    at com.logmein.assets.orchestrationservice.configuration.GraphQLConfiguration$$EnhancerBySpringCGLIB$$22d7c6c0.CGLIB$jsonType$1(<generated>)
    at com.logmein.assets.orchestrationservice.configuration.GraphQLConfiguration$$EnhancerBySpringCGLIB$$22d7c6c0$$FastClassBySpringCGLIB$$35cc9cfe.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
    at com.logmein.assets.orchestrationservice.configuration.GraphQLConfiguration$$EnhancerBySpringCGLIB$$22d7c6c0.jsonType(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 93 common frames omitted

Can anybody please suggest what is missing/wrong in the way I am using it? Any help is appreciated.


Solution

  • Caused by: java.lang.NoClassDefFoundError: graphql/schema/CoercingParseLiteralException
        at graphql.scalars.datetime.DateTimeScalar.<init>(DateTimeScalar.java:27)
        at graphql.scalars.ExtendedScalars.<clinit>(ExtendedScalars.java:40)
    

    graphql-java-extended-scalars requires the class CoercingParseLiteralException which is introduced since graphql-java 8.0. However , you are using a very old version of graphql-java which is 6.0 , hence it does not have this class and throw NoClassDefFoundError.

    The solution is to update both graphql-spring-boot-starter and graphql-java-tools versions such that they are using at the same version of graphql-java which is at least version 8.

    You can use mvn dependency:tree to check which version of graphql-java is included.