File Structure screenshot
I’ve seen other posts solving this issue with this exact file structure yet no matter what I do it always gives me this error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.redacted.project.service.MessageServiceImpl required a bean of type 'com.example.redacted.project.repositories.MessageRepository' that could not be found.
Action:
Consider defining a bean of type 'com.example.redacted.project.repositories.MessageRepository' in your configuration.
Process finished with exit code 1
I’ve tried the structure of the “solution” post here: ‘Field required a bean of type that could not be found.’ error spring restful API using mongodb
to no avail
The only thing that has worked is having both the main application and the repository in its own subfolder but then the application can’t find anything else and I run into other issues.
The repository has the @Repository annotation, the Service has the @Service annotation, I’ve tried using the @ComponentScan annotation to no avail.
Spring main class:
package com.example.redacted.project;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableAsync
public class Application extends SpringBootServletInitializer implements WebMvcConfigurer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Message Repository:
package com.example.redacted.project.repositories;
import com.example.redacted.project.model.Message;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface MessageRepository extends CrudRepository<Message, Long> {
}
Message Service:
package com.example.redacted.project.service;
import com.example.redacted.project.model.Message;
public interface MessageService {
public Message getMessage();
public void setMessage(String message);
}
Message Service Impl:
package com.example.redacted.project.service;
import com.example.gegg.project.model.Message;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.example.redacted.project.repositories.MessageRepository;
@RequiredArgsConstructor
public class MessageServiceImpl implements MessageService {
private final MessageRepository messageRepository;
@Override
public Message getMessage() {
long id = 1;
String message = String.valueOf(messageRepository.findById(id));
return new Message();
//return message.orElse(null);
}
@Override
public void setMessage(String message) {
}
}
Message Controller:
package com.example.redacted.project.controller;
import com.example.redacted.project.model.Message;
import com.example.redacted.project.service.MessageService;
import com.example.redacted.project.support.LogEndoint;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
public class MessageController {
private final MessageService messageService;
@LogEndoint
@GetMapping("/message")
public ResponseEntity<Message> getMessage(){
return new ResponseEntity<>(messageService.getMessage(), HttpStatus.OK);
}
@LogEndoint
@GetMapping("/status")
public ResponseEntity<String> getStatus(){
return new ResponseEntity<>("Service is good to go",HttpStatus.OK);
}
}