【问题标题】:How do I work out what dependency I need for Spring Boot MultiValueMap in Gradle?如何计算 Gradle 中 Spring Boot MultiValueMap 需要的依赖项?
【发布时间】:2018-02-16 07:41:03
【问题描述】:

我有一些代码需要访问org.springframework.util.MultiValueMap

我在 Windows 机器上运行 IntelliJ IDEA。我收到一个编译错误:

“无法访问 org.springframework.util.MultiValueMap”。

这个错误出现在以下代码行(request是ClientHttpRequest类型的对象),在junit测试文件中:

String authorization = request.getHeaders().getFirst("Authorization");  

根据 MultiValueMap (https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/MultiValueMap.html) 的文档,我尝试了以下两个导入语句:

import org.springframework.util;
import org.springframework.util.MultiValueMap;

但是这些不起作用 - 这些语句的后半部分以红色突出显示。我相信这是因为我在 build.gradle 中没有正确的依赖项。

这些是我目前拥有的 Spring Framework 依赖项:

compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.ws:spring-ws-core")
compile('org.springframework.security:spring-security-web:5.0.2.BUILD-SNAPSHOT')
compile('org.springframework.security:spring-security-config:5.0.2.BUILD-SNAPSHOT')
compile('org.springframework.security:spring-security-config:5.0.2.BUILD-SNAPSHOT')
compile("org.springframework.security.oauth:spring-security-oauth2:2.0.8.RELEASE")
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.ws:spring-ws-test')

【问题讨论】:

  • 1. org.springframework.util.MultiValueMap 2. 太宽泛,无法回答,如果你做的一切都正确,它不应该发生,如果它仍然发生,请尝试谷歌 3。不,它应该是 import org.springframework.util.MultiValueMap;
  • 为什么不用spring-boot依赖呢?
  • @Oleg:我所说的依赖是指 build.gradle 文件中的依赖部分。例如编译(“org.springframework.boot:spring-boot-starter-web”)。在过去的半个小时里,我一直在谷歌上搜索这个,我找不到任何有帮助的东西。你说“如果你做的一切都是对的”......显然我没有做对所有事情,但我无法弄清楚我做错了什么。我尝试了您的建议(import org.springframework.util.MultiValueMap;)但这也不起作用,根据 MultiValueMap 的文档(我的帖子中的链接)我认为 org.springframework.util 应该足够了。
  • @Christian:我不确定你的意思。我已经有几个 springframework 依赖项,但它们似乎还不够。我将更新我的帖子以添加更多信息。

标签: java spring spring-boot gradle dependencies


【解决方案1】:

我找到了解决办法!

答案在此页面上:https://spring.io/blog/2015/02/23/better-dependency-management-for-gradle

似乎使用 gradle 来管理 Spring-Boot 的依赖项可能会出现问题,因为您的依赖项部分需要太多行,就像我上面列出的那样。

我按照该页面上的说明,将以下行插入到我的 build.gradle 中:

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

dependencyManagement {
    imports {
        mavenBom 'io.spring.platform:platform-bom:1.1.1.RELEASE'
    }
}

我的代码现在可以编译了。我仍然不知道在哪里可以找到正确的 build.gradle 依赖项,但使用这个插件不再是问题。

【讨论】:

    【解决方案2】:

    可能在这里做了太多的工作。或者,您会冒着使依赖项稍微不同步的风险。

    您需要的唯一依赖项是spring-boot-starter,它具有到您想要的正确spring-core 版本的传递依赖项。

    您可以在 build.gradle 文件中使用它:

    compile'org.springframework.boot:spring-boot-starter:1.5.10.RELEASE'
    

    如果您想为 Spring Boot 使用 Gradle 插件(can be found here),请将您想要的 Spring Boot 版本放入插件部分并省略依赖项中的版本。

    【讨论】:

    • 如果你使用 Gradle 的 Spring Boot 插件,它将覆盖它关心的依赖版本。
    【解决方案3】:

    这条线会排在你班级的顶部

    import org.springframework.util.MultiValueMap;
    

    虽然我不一定熟悉这个确切的库,但查看 mvnrepository 告诉我,您的 build.gradle 文件中需要类似的东西

    repositories {
        mavenCentral() 
    }
    
    dependencies {
        compile group: 'org.springframework', name: 'spring-core', version: '5.0.3.RELEASE'
    }
    

    【讨论】:

    • 您好,您为我的 build.gradle 中的依赖项部分建议的语法似乎不正确。它不会编译。我的其他依赖项如下所示:compile("org.springframework.boot:spring-boot-starter-web")。请你能给我更多关于如何浏览 mvnrepository 以找到正确依赖项的信息吗?我已经尝试搜索 MultiValueMap,但没有得到任何结果。
    • 嗯,这是我自己习惯的语法。我实际上只是从我在答案中输入的链接中直接复制了这个。所以,对不起,不能帮你。我通常只是谷歌“maven repository com.name.of.the.thing”,并没有真正遇到问题。您是否使用clean build 任务重新构建了项目
    • 我认为您要查找的语法是compile('org.springframework:spring-core:5.0.3-RELEASE')。你习惯的方式看起来像 compile('group:name:version') 格式,所以我会尝试一下
    • 我可以使用您建议的语法添加单独的依赖项部分,并且没有语法错误......但不能解决我原来的问题。但是,我找到了一个修复它的插件(请参阅上面的答案)。无论如何感谢您的帮助!
    • Gradle 对声明依赖有灵活的支持,j.seashell 的语法也是正确的。事实上,原始问题中使用的语法不是惯用的,因为括号通常被省略并使用单引号 (') 而不是双引号 ("),因为只有在进行字符串插值时才需要双引号。跨度>
    猜你喜欢
    • 2016-09-17
    • 1970-01-01
    • 2020-03-31
    • 2019-07-26
    • 1970-01-01
    • 2015-06-28
    • 2022-01-17
    • 2018-01-31
    • 2022-12-23
    相关资源
    最近更新 更多