4

I am trying to use Gson to send JsonObjects as response. These JsonObjects are constructed manually instead of using objectMapper or Gson. I excluded all the dependencies related to Jackson. It works well and good. Later I tried to add Swagger documentation using springfox. Now it throws below exceptions:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectMapperConfigurer' defined in springfox.documentation.spring.web.SpringfoxWebMvcConfiguration: Post-processing of merged bean definition failed; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:526) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:235) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:703) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:528) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at com.honeywell.ldi.LdiDetailerApplication.main(LdiDetailerApplication.java:44) ~[classes/:na]
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_151]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_151]
    at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_151]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:613) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:489) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.buildLifecycleMetadata(InitDestroyAnnotationBeanPostProcessor.java:206) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.findLifecycleMetadata(InitDestroyAnnotationBeanPostProcessor.java:187) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(InitDestroyAnnotationBeanPostProcessor.java:125) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(CommonAnnotationBeanPostProcessor.java:295) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:992) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:523) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    ... 15 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_151]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_151]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[na:1.8.0_151]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_151]
    ... 26 common frames omitted
Divya
  • 83
  • 8
  • Possible duplicate of [springfox(swagger2) does not work with GsonHttpMessageConverterConfig](https://stackoverflow.com/questions/30219946/springfoxswagger2-does-not-work-with-gsonhttpmessageconverterconfig) – Darren Forsythe Nov 21 '17 at 18:53

1 Answers1

1

The exception is due to springfox.documentation.spring.web.ObjectMapperConfigurer complaining about missing com.fasterxml.jackson.databind.ObjectMapper.

The ObjectMapper exists in com.fasterxml.jackson.core:jackson-databind:jar. You would still need jackson-databind JAR to make your application work with Springfox.

As for SpringFox, it doesn't work with Gson out of the box. You need to register custom serializers for different Springfox artifacts with the Gson object.

Gson gson = GsonBuilder()
    // needed for making calls to /v2/api-docs
    .registerTypeAdapter(Json.class, 
        new SpringfoxJsonSerializer())
    // rest are needed for making calls to /swagger-ui.html
    .registerTypeAdapter(ApiListing.class, 
        new SpringfoxApiListingJsonSerializer())
    .registerTypeAdapter(SwaggerResource.class, 
        new SpringfoxResourceJsonSerializer())
    .registerTypeAdapter(ResourceListing.class, 
        new SpringfoxResourceListingJsonSerializer())
    .registerTypeAdapter(UiConfiguration.class, 
        new SpringfoxUiConfigurationJsonSerializer())
    // needed if you have security
    .registerTypeAdapter(SecurityConfiguration.class, 
        new SpringfoxSecurityConfigurationJsonSerializer())
    ...
    .create();

Here is a working example with Spring Boot, SpringFox, and Gson.

Indra Basak
  • 7,124
  • 1
  • 26
  • 45
  • Thank you Indra Basak. This is helpful. I used SpringfoxJsonSerializer() and with this the /v2/api-docs works for me. However, the swagger-ui won't appear as expected and it keeps on saying fetching the resources. I noticed a strange thing that when I remove configureMessageConverters for GsonHttpMessageConverters, the API is available but won't work for custom constructed JsonOjects. I appreciate your help in this. – Divya Nov 27 '17 at 20:27
  • You need the rest of JSON serializers to make your UI work. Please check the project I referred to. – Indra Basak Nov 27 '17 at 20:45