【问题标题】:BodyInserters.fromObject is deprecated, what is the alternative?BodyInserters.fromObject 已弃用,有什么替代方案?
【发布时间】:2023-03-13 03:27:01
【问题描述】:

你好。 我正在尝试使用 Spring webclient 发送发布请求并用对象填充正文。但我收到以下“'fromObject(T)' 已弃用”。 那还有什么办法呢?

WebClient.post()
            .uri("example.com")
            .body(BodyInserters.fromObject(newObject);

【问题讨论】:

    标签: java spring-boot spring-webclient


    【解决方案1】:

    javadoc 明确表示使用fromValue

    public static <T> BodyInserter<T,ReactiveHttpOutputMessage> fromValue(T body)
    

    插入器写入给定的值。 或者,考虑在 WebClient 和 ServerResponse 上使用 bodyValue(Object) 快捷方式。

    类型参数:

    T - 正文的类型

    参数: body - 要写入的值

    返回:

    插入器写入单个值

    投掷:

    IllegalArgumentException - 如果 body 是 Publisher 或 ReactiveAdapterRegistry.getSharedInstance() 支持的类型的实例,则应使用 fromPublisher(Publisher, Class)fromProducer(Object, Class)

    【讨论】:

    • 非常感谢。更改为 WebClient.post() .uri("example.com") .body(.fromValue( object); 成功了
    猜你喜欢
    • 2016-08-16
    • 2022-10-05
    • 2022-06-10
    • 2022-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 2013-09-26
    相关资源
    最近更新 更多