I am aware that there are multiple questions with similar premises with this one. however - I have read all of them and most of them either has been left unanswered or the answer is not applicable to my situation.
I am building an API using MVC infrastructure. I have create a service in this particular way
public interface KurirService {
Map<String,Object> waBlast(Map<String,Object> data);
}
@Service
public class KurirServiceImpl implements KurirService{
public KurirServiceImpl() {
System.out.println("KurirServiceImpl");
}
@Autowired
private JatisService jatisService;
@Autowired
private DotEnvConstant dotEnvConstant;
@Autowired
private RSAEngineService rsaEngineService;
@Override
public Map<String, Object> waBlast(Map<String, Object> data) {
//code here
}
and this is my Controller
@RestController
@CrossOrigin(allowedHeaders = "*")
@RequestMapping("/kurir")
public class KurirController {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private CardMonitoringRepository cardMonitoringRepository;
@Autowired
private AuditTrailRepository auditTrailRepository;
@Autowired
private TrackingStatusRepository trackingStatusRepository;
@Autowired
private AuditTrailService auditTrailService;
@Autowired
private FastService fastService;
@Autowired
private KurirService kurirService;
@Autowired
private DeliveryTrackingRepository deliveryTrackingRepository;
@Autowired
private JatisService jatisService;
@Autowired
private TrackingHistoryRepository trackingHistoryRepository;
@Autowired
private ListKurirRepository listKurirRepository;
@Autowired
private RSAEngineService rsaEngineService;
@PostMapping("wa-blast")
private Map<String,Object> waBlast(@RequestBody Map<String,Object> data){
System.out.println("data = " + data);
Map<String,Object> response = new HashMap<>();
String noHp = (String) data.get("noHp");
if(noHp == null) {
response.put("rc", "01");
response.put("message", "noHp is null");
return response;
}
if (kurirService == null) {
response.put("rc", "02");
response.put("message", "kurirService is null");
return response;
}
Map<String,Object> whatsapp = kurirService.waBlast(data);
response = whatsapp;
return response;
}
}
However, when I hit the endpoint with valid requestBody, it gives me this response
{
"rc": "02",
"message": "kurirService is null"
}
Which means that kurirService is null and the autowired is not successful for some reason.
I have checked that the component is scanned properly when the app is initialized at the beginning by using this method within my SpringBootApplication
@SpringBootApplication
public class CpdmKurirApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(CpdmKurirApplication.class, args);
int contextCount = countApplicationContexts(context);
System.out.println("Number of Application Contexts: " + contextCount);
Map<String, KurirService> kurirServiceBeans = context.getBeansOfType(KurirService.class);
System.out.println("KurirService beans: " + kurirServiceBeans.keySet());
}
private static int countApplicationContexts(ApplicationContext context) {
int count = 0;
while (context != null) {
count++;
context = context.getParent();
}
return count;
}
@Bean
public WebMvcConfigurer corsMappingConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("localhost:9005")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*");
}
};
}
}
There is also no multiple application context that is being made which could be a probability of the error and kurirService bean is created successfully. I have made multiple similar API using this approaches and this is my first time encountering this kind of problem which leave me stuck.
Any kind of advice and answer is appreciated!
//edit: The rest autowired and PersistenceContext is used for other endpoint that I decide to omit but I leave the autowire there in the case that it is a necessary info