Search code examples
javaspringkafka-producer-apiendpoint

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. 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.

In my Endpoint.java I've a methode for 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<>

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.


Solution

  • 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.

    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.

    • SECTION 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:

    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:

    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, 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:

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

    enter image description here

    I sincerely hope I communicated well, fell free to ask me any question.