首页 > 解决方案 > 使用 com.rabbitmq.http.client.Client 从 Java 向 RabbitMQ 发送消息

问题描述

我正在尝试将消息从 Java 服务发送到 RabbitMQ。

我正在使用一些 Java RabbitMQ 客户端库并尝试运行以下代码:

    private static Client setAcceptAllSSL() throws Exception {
        TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
        SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
        requestFactory = new HttpComponentsRestTemplateConfigurator(sslsf,sslContext);
        return new Client(new ClientParameters().url(url).username(username).password(password).restTemplateConfigurator(requestFactory));
    }

在最后一行(客户端对象初始化)中,抛出以下错误:

java.lang.NoClassDefFoundError: org/springframework/http/converter/json/Jackson2ObjectMapperBuilder

我想我的 pom.xml 中可能缺少某些内容,或者可能是 Spring 版本中的某些内容。但是,我找不到任何缺少的导入/库/版本问题。

请帮忙 :)

标签: javaspringrabbitmqspring-rabbit

解决方案


在 pom.xml 中添加此 jackson 依赖项:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.databind-version}</version>
</dependency>

推荐阅读