0

So i want to save the data with the help of JPA repository but when i autowired it is giving me null. I don't know what else to do in here I have also tried with @EnableJPARepository annotation still no help. So here are the classes i am using. I want to save the data that is published by kafka so is it best to create a seperate function or can i put save to db function in consume json?

Repository

package com.prateek.addaKafka.addaKafka.kafkaConsumer;

import org.springframework.data.repository.CrudRepository;


public interface KafkaRepository extends CrudRepository<KafkaEntity,Long> {
}

KafkaEntityClass

package com.prateek.addaKafka.addaKafka.kafkaConsumer;

import javax.persistence.*;

@Entity
@Table(name = "messages")
public class KafkaEntity {
    @Id
    @GeneratedValue
    private Long id;
    @Column(nullable = false)
    private String userName;
    @Column(nullable = false)
    private String userMessage;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserMessage() {
        return userMessage;
    }

    public void setUserMessage(String userMessage) {
        this.userMessage = userMessage;
    }
}

ServiceClass

package com.prateek.addaKafka.addaKafka.kafkaConsumer;

import com.prateek.addaKafka.addaKafka.model.UserPublishModel;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class KafkaListenerService {

    @Autowired
    KafkaRepository kafkaRepository;

    @KafkaListener(topics = "NewTopic",groupId = "group_json",
            containerFactory = "userKafkaListenerFactory")
    public UserPublishModel consumeJson(UserPublishModel userPublishModel) {
        return userPublishModel;
    }

    public void saveToDB(UserPublishModel userPublishModel){
        ModelMapper modelMapper=new ModelMapper();
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
        KafkaEntity kafkaEntity=modelMapper.map(userPublishModel, KafkaEntity.class);
        kafkaRepository.save(kafkaEntity);
    }

}

MainClass

package com.prateek.addaKafka.addaKafka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
public class AddaKafkaApplication {

    public static void main(String[] args) {
        SpringApplication.run(AddaKafkaApplication.class, args);
    }

}
Vortex
  • 70
  • 6
  • An `@Autowired` field simply cannot be `null`. Spring will fail to even start if it cannot fulfil the dependency. Hence it cannot be `null`. If it is `null` then you aren't showing the actual code you are using (but probably a dumbed down version). See https://deinum.biz/2020-07-03-Autowired-Field-Null/ for reasons. Tempting to vote this as a duplicate of https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null – M. Deinum Jun 14 '21 at 14:06
  • `null at com.prateek.addaKafka.addaKafka.kafkaConsumer.KafkaListenerService.saveToDB(KafkaListenerService.java:26) ~[classes/:na] at com.prateek.addaKafka.addaKafka.addaController.KafkaPublishController.publishJsonMessage(KafkaPublishController.java:34) ` This was the error which occur when i debug it shows kafkarepository as null. This savetoDb function only trigger when i hit a post request to an end point. So when iam doing this this kafkarepo instance is coming null. – Vortex Jun 14 '21 at 14:09
  • It isn't. When debugging you are looking at the proxy **not** the actual instance of the bean. – M. Deinum Jun 14 '21 at 14:20
  • So what should i do to solve this problem can not really figure it out. – Vortex Jun 14 '21 at 14:24
  • 1
    *kafkaEntity* is probably null – Dirk Deyne Jun 14 '21 at 14:33
  • Can you show the code, where you call the `saveToDB` method (from the stacktrace it should be the class `KafkaPublishController`)? – dunni Jun 14 '21 at 15:01
  • @Vortex how is code executed? Do you use tests? Are you instantiating service manually? Or are you running the app and use some other tool to send requests? – Stefan Golubović Jun 14 '21 at 19:40
  • do you have the same error when you annotate KafkaRepository @Repository? – tremendous7 Jun 15 '21 at 00:09

0 Answers0