简体   繁体   English

错误:无法推断 ProducerRecord<> 的类型 arguments

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

(I'm sorry if you see some english problem, I'm french !) (如果你看到一些英语问题,我很抱歉,我是法国人!)

I have a problem with a method in a Java Projet using kafka.我对使用 kafka 的 Java Projet 中的方法有疑问。 I've got a database of prices and i want to send to kafka a message with all the information of a price when I delete a price.我有一个价格数据库,我想在删除价格时向 kafka 发送一条包含价格所有信息的消息。

In my Endpoint.java I've a methode for deleteById(idPrix):在我的 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;
           });

           }

I got this message: cannot infer type arguments for ProducerRecord<>我收到此消息:无法推断 ProducerRecord<> 的类型 arguments

I've tried so much different things to make it work but no success.我已经尝试了很多不同的方法来使它工作,但没有成功。 If someone see what's the problem it will be great.如果有人看到问题出在哪里,那就太好了。

hope this is not too late, just want for future references.希望这还不算晚,只是希望将来参考。 Skip to SECTION 2 for the actual answer, SECTION 1 tries to explain what the error message means in general sense.跳到第 2 节以获得实际答案,第 1 节试图解释错误消息的一般意义。

So, whenever you come across cannot infer type arguments for something... it means you're parsing a wrong type and that obviously won't work.因此,每当您遇到cannot infer type arguments for something...这意味着您正在解析错误的类型,这显然是行不通的。

  • SECTION 1:第 1 部分:

In your case, let try to understand the snippet piece by piece: Mono<Void> ---- This means that Mono is a class that accept a generic Type and that means you could parse Void also and if you parse Void, you can't parse a non Void data type, to be clear, let assume your Mono<Void> class look like below:在您的情况下,让我们尝试逐个理解片段: 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 ....
    }

So, like you try using it above: Mono<Void> it means you can only do something like below:因此,就像您在上面尝试使用它一样: 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> . 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> . If, let assume you don't want to parse anything at all as the third argument, then you can overload the constructor and eliminate the third the argument.如果假设您根本不想将任何内容解析为第三个参数,那么您可以重载构造函数并消除第三个参数。

  • SECTION 2:第 2 部分:

Now facing the main issue, this concept is also applied to ProducerRecord<String, Price> : Does ProducerRecord<String, Price> has a constructor that satisfy the implementation above?现在面对主要问题,这个概念也适用于ProducerRecord<String, Price>ProducerRecord<String, Price>是否有满足上述实现的构造函数? new ProducerRecord<>(TOPIC, idPrix.toString(), data) ; new ProducerRecord<>(TOPIC, idPrix.toString(), data) ; The constructor should look something like this:构造函数应如下所示:

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.*/
    }

在此处输入图像描述

I sincerely hope I communicated well, fell free to ask me any question.我真诚地希望我能很好地沟通,请随时问我任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM