如何在 Gradle 项目中使用 SpringFox 2.9.2 和 Spring HATEOAS 2+

几周前,我开始了一个新项目,在这个项目中,我遇到了使用Spring HATEOAS的挑战。我认为这很容易实现。事实上是这样的,因为Spring项目有很好的文档。我没有预料到的是,Spring Fox 2.9.2和Spring HATEOAS 2+是不兼容的。这是由于Spring Fox使用了Spring Plugin Core (1.2.0.RELEASE),

在寻找解决方案时,我可以在Spring Fox github存储库中找到以下问题:

springfox-swagger2版本2.9.2与springboot版本2.2.0不兼容。M4 # 3052

哈喽,

我用的是swagger2 2.9.2, springboot 2.2.0。M4, Hateos 0.25.1.RELEASE。下面是我得到的rumtime错误:

尝试调用不存在的方法。这次尝试是在以下地点进行的:

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

" 面的方法不存在:

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

方法的类,org.springframework.plugin.core。PluginRegistry,可从以下位置获得:

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

它是从以下位置加载的:

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版本"

以下是我的级配置:

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() }

如果我像下面这样改变springboot版本,那么问题就消失了。

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

 
这个页面上的一个解决方案说我们应该包含Spring Plugin Core 1.2.0.版本依赖。

但是我正在使用gradle,它具有自己的依赖项管理规则。 由于这个事实,我们必须强制如下下载依赖项:

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

添加依赖项后,该项目仍无法运行,甚至无法启动。 为了解决该问题,我不得不进行更多搜索。 最后,我必须在项目中使用自己的实现覆盖Spring HATEOAS的“ discovers” bean。

@SpringBootApplication
public class HateoasBean {

  @Bean
  public LinkDiscoverers discoverers(){
    return new LinkDiscoverers(SimplePluginRegistry.create(Arrays.asList(new CollectionJsonLinkDiscoverer())));
  }
}

只有这样,我才能成功地将Spring Fox和Spring HATEOAS结合使用。我创建这篇文章是为了简化其他正在为同样的问题寻找解决方案的开发人员。希望它对某些人有用。

原文链接: https://dev.to//otaviotarelhodb/how-to-use-springfox-2-9-2-with-spring-hateoas-2-on-gradle-project-6mn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值