在编写 SpringBoot 应用程序的时候,需要发送 http 请求,尝试引入以下代码,发现根本没有引入成功。

<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>5.2.1</version>
</dependency>

排查了很久也没有成功,也不是 idea 的 kotlin 编译器过旧的问题。在重新查阅官方文档后发现以下文字。

Maven and JVM Projects

OkHttp is published as a Kotlin Multiplatform project. While Gradle handles this automatically, Maven projects must select between okhttp-jvm and okhttp-android. The okhttp artifact will be empty in Maven projects.

也就表明 okhttp 包直接引入 maven 是空的。必须编写以下引入才会成功引入。

        <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/logging-interceptor -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>logging-interceptor</artifactId>
            <version>5.2.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp-bom -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp-bom</artifactId>
            <version>5.2.1</version>
            <type>pom</type>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/mockwebserver3 -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>mockwebserver3</artifactId>
            <version>5.2.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp-jvm -->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp-jvm</artifactId>
            <version>5.2.1</version>
        </dependency>

记述以下以上这些 maven 包的作用。

logging-interceptor:Interceptors are a powerful mechanism that can monitor, rewrite, and retry calls. Here’s a simple interceptor that logs the outgoing request and the incoming response.

mockwebserver3:okhttp3:mockwebserver3 is a testing library from Square that acts as a scriptable, in-memory web server for testing applications that use the OkHttp client.

okhttp-box:okhttp-bom is a Bill of Materials (BOM) from Square that manages the versions of all related OkHttp artifacts to ensure compatibility in projects with many dependencies.

okhttp-jvm:okhttp-jvm is the specific artifact for the OkHttp library that is built for Java Virtual Machine (JVM) environments, as opposed to the okhttp-android artifact, which is tailored for Android.

更多推荐