DEV Community

Otavio Tarelho
Otavio Tarelho

Posted on • Updated on

How to use SpringFox 2.9.2 with Spring HATEOAS 2+ on a Gradle Project

A few weeks ago, I started a new project where I was challenged to use Spring HATEOAS. Well, I thought that would be really simple to implement. In fact it is, since Spring projects are well documented. What I wasn't expecting was that Spring Fox 2.9.2 and Spring HATEOAS 2+ were not compatible. This is due to the fact that Spring Fox uses the Spring Plugin Core (1.2.0.RELEASE) while Spring HATEOAS uses a newer one.

Searching for a solution, I could find the following issue on Spring Fox github repository:

springfox-swagger2 version 2.9.2 not compatible with springboot version 2.2.0.M4 #3052

Hi,

I am using swagger2 2.9.2, springboot 2.2.0.M4, Hateos 0.25.1.RELEASE. Below is the rumtime error I'm getting:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

springfox.documentation.spring.web.plugins.DocumentationPluginsManager.createContextBuilder(DocumentationPluginsManager.java:152)

" The following method did not exist:

org.springframework.plugin.core.PluginRegistry.getPluginFor(Ljava/lang/Object;Lorg/springframework/plugin/core/Plugin;)Lorg/springframework/plugin/core/Plugin;

The method's class, org.springframework.plugin.core.PluginRegistry, is available from the following locations:

jar:file:/C:/Users/EKHTJHD/.gradle/caches/modules-2/files-2.1/org.springframework.plugin/spring-plugin-core/2.0.0.M1/189f78af81f23eef12018a4d4cf50b8a6df8ec0d/spring-plugin-core-2.0.0.M1.jar!/org/springframework/plugin/core/PluginRegistry.class

It was loaded from the following location:

file:/C:/Users/EKHTJHD/.gradle/caches/modules-2/files-2.1/org.springframework.plugin/spring-plugin-core/2.0.0.M1/189f78af81f23eef12018a4d4cf50b8a6df8ec0d/spring-plugin-core-2.0.0.M1.jar

Action: Correct the classpath of your application so that it contains a single, compatible version of org.springframework.plugin.core.PluginRegistry

"

Below is my gradle configuration:

plugins { id 'org.springframework.boot' version '2.2.0.M4' //id 'org.springframework.boot' version '2.1.6.RELEASE' id 'java' }

apply plugin: 'io.spring.dependency-management'

group = 'com.in28minutes.rest.webservices' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8'

configurations { developmentOnly runtimeClasspath { extendsFrom developmentOnly } }

repositories { mavenCentral() maven { url 'https://repo.spring.io/snapshot' } maven { url 'https://repo.spring.io/milestone' } }

dependencies { //implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' compile group: 'org.springframework.hateoas', name: 'spring-hateoas', version: '0.25.1.RELEASE' compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2' compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2' //developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'com.h2database:h2' testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' exclude group: 'junit', module: 'junit' } }

test { useJUnitPlatform() }

If I change springboot version as below then the issue disappears.

id 'org.springframework.boot' version '2.1.6.RELEASE'

One of the solutions from this page says that we should include Spring Plugin Core 1.2.0.RELEASE as dependency.

However I'm using gradle which has its own dependency management rules. Due to this fact, we must force the dependency download as bellow:

compile("org.springframework.plugin:spring-plugin-core:1.2.0.RELEASE") { force = true }

After adding the dependency, the project wasn't unable to run yet, not even starting. I had to do more search in order to fix that. At the end, I had to override the "discovers" bean from Spring HATEOAS with my own implementation on my project.

Only after that I could successfully use Spring Fox and Spring HATEOAS together. I created this post to simplify to other devs that are searching for a solution for the same problem. Hopefully It will be useful for someone.

Top comments (2)

Collapse
 
iviel profile image
iVieL

Thank you, 'force=true' makes the work for me.

Collapse
 
fatezhang profile image
Jiaheng

Just curious why you don't upgrade the version to 3.0.0, is there any issue when doing that?