簡體   English   中英

錯誤:無法推斷 ProducerRecord<> 的類型 arguments

[英]Error : cannot infer type arguments for ProducerRecord<>

(如果你看到一些英語問題,我很抱歉,我是法國人!)

我對使用 kafka 的 Java Projet 中的方法有疑問。 我有一個價格數據庫,我想在刪除價格時向 kafka 發送一條包含價格所有信息的消息。

在我的 Endpoint.java 中,我有一個 deleteById(idPrix) 的方法:

        @DeleteMapping
        @RequestMapping(value ="/delete{idPrix}")

        public Mono<Void> deleteById (@RequestParam(required = true, name = "idPrix") Long idPrix){

       return priceservice.deleteById(idPrix).map( data -> {
                ProducerRecord<String, Price>  producerRecord = new ProducerRecord<>(TOPIC, idPrix.toString(), data );
                kafkaTemplate.send(producerRecord);
                return null;
           });

           }

我收到此消息:無法推斷 ProducerRecord<> 的類型 arguments

我已經嘗試了很多不同的方法來使它工作,但沒有成功。 如果有人看到問題出在哪里,那就太好了。

希望這還不算晚,只是希望將來參考。 跳到第 2 節以獲得實際答案,第 1 節試圖解釋錯誤消息的一般意義。

因此,每當您遇到cannot infer type arguments for something...這意味着您正在解析錯誤的類型,這顯然是行不通的。

  • 第 1 部分:

在您的情況下,讓我們嘗試逐個理解片段: Mono<Void> ---- 這意味着 Mono 是一個接受通用類型的 class ,這意味着您也可以解析 Void 如果您解析 Void,您可以'不解析非 Void 數據類型,為了清楚起見,假設您的Mono<Void> class 如下所示:

public class Mono<T> {
    private boolean status;
    private String message;
    private T data;
    
     //....getter or setter ....
    }

因此,就像您在上面嘗試使用它一樣: Mono<Void>這意味着您只能執行以下操作:

return Mono<>(true, "Success", /*You can only parse null here*/)

If you want to parse Object rather than null there then you will have to change Mono<Void> to Mono<Object> note "Object" is the Class name of the new Object you want it to return in your case ProducerRecord Mono<ProducerRecord> . 如果假設您根本不想將任何內容解析為第三個參數,那么您可以重載構造函數並消除第三個參數。

  • 第 2 部分:

現在面對主要問題,這個概念也適用於ProducerRecord<String, Price>ProducerRecord<String, Price>是否有滿足上述實現的構造函數? new ProducerRecord<>(TOPIC, idPrix.toString(), data) ; 構造函數應如下所示:

public ProducerRecord(Topic topic, String idPrix, Price data){ /*Note Topic might be String in your case just make sure you're parsing the right data-type, and data you're parsing must be of type Price.*/
    }

在此處輸入圖像描述

我真誠地希望我能很好地溝通,請隨時問我任何問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM