0

i have got this Exception

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dumpController' defined in file [...\tech\backend\webapplication\com\controller\DumpController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'dumpService' defined in file [...\tech\backend\webapplication\com\service\DumpService.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'dumpRepository' defined in tech.backend.webapplication.com.repo.DumpRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class tech.backend.webapplication.com.model.Dump
...


Caused by: java.lang.IllegalArgumentException: Not a managed type: class tech.backend.webapplication.com.model.Dump
...

Process finished with exit code 1

Here are my classes:

MainClass

package tech.backend.webapplication;

@SpringBootApplication
public class WebapplicationApplication {

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

}

ModelClass:

package tech.backend.webapplication.com.model;


@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
public class Dump implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String email;
    
}

Repository:

package tech.backend.webapplication.com.repo;

@Repository
public interface DumpRepository extends JpaRepository<Dump, Long> {
    void deleteDumpById(Long id);
    Optional<Dump> findDumpById(Long id);
}

Service:

package tech.backend.webapplication.com.service;

@Service
@RequiredArgsConstructor
@Slf4j
@Transactional
public class DumpService {

    private final DumpRepository dumpRepository;

    public Dump addDump(Dump dump) {
        dump.setName("Johannes");
        dump.setEmail("Johannes@mail.com");
        return dumpRepository.save(dump);
    }

    public List<Dump> getAllDumps() {
        return dumpRepository.findAll();
    }

    public Dump getDumpById(Long id) {
        return dumpRepository.findDumpById(id)
                .orElseThrow(() -> new DumpNotFoundException("Dump by id " + id + "was not found"));
    }

    public Dump updateDump(Dump dump) {
        return dumpRepository.save(dump);
    }

    public void deleteDump(Long id) {
        dumpRepository.deleteDumpById(id);
    }
}

Controller:

package tech.backend.webapplication.com.controller;

@RestController
@RequestMapping("/dump")
@RequiredArgsConstructor
@Slf4j
public class DumpController {

    private final DumpService dumpService;

    @GetMapping("/all")
    public ResponseEntity<List<Dump>> getAllDumps() {
        List<Dump> dumps = dumpService.getAllDumps();
        return new ResponseEntity<>(dumps, HttpStatus.OK);
    }

    @GetMapping("/find/{id}")
    public ResponseEntity<Dump> getDumpById(@PathVariable("id") Long id) {
        Dump dump = dumpService.getDumpById(id);
        return new ResponseEntity<>(dump, HttpStatus.OK);
    }

    @PostMapping("/add")
    public ResponseEntity<Dump> addDump(@RequestBody Dump dump) {
        Dump newDump = dumpService.addDump(dump);
        return new ResponseEntity<>(newDump, HttpStatus.CREATED);
    }

    @PutMapping("/update")
    public ResponseEntity<Dump> updateDump(@RequestBody Dump dump) {
        Dump updatedDump = dumpService.updateDump(dump);
        return new ResponseEntity<>(updatedDump, HttpStatus.CREATED);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<?> deleteDump(@PathVariable("id") Long id) {
        dumpService.deleteDump(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}

The packages are as follows:

  • java
    • tech.backend.application
    • com
      • controller
      • exception
      • model
      • repo
      • service
    • WebapplicationApplication (class)
  • ressources

Can somebody help me to fix this error? I would appreciate that :)

Thanks

Every time I start the application, this exception is the output

JoHannes
  • 1
  • 2
  • I think I once read something about JPA and Lombok needing some extra steps, but I don't use Lombok so I can't recall it exactly. You could try looking around from that angle. Sorry I can't be more clear. – Jetto Martínez Feb 18 '23 at 16:54
  • I kicked Lombok out of my project to test it, but nothing changed. I don't know :/ – JoHannes Feb 18 '23 at 17:08
  • You added the `@Autowired` or generated the appropriate constructor for the injection in your Controller and Service after removing Lombok, correct? Just making sure the error hasn't changed. – Jetto Martínez Feb 18 '23 at 17:23
  • I copy-pasted the code you provided in a demo project with an in-memory database and Java 11. It ran first try, with no modifications nor extra annotations added. This leads me to believe that the problem is in your environment, not the code. – Jetto Martínez Feb 18 '23 at 17:47

2 Answers2

0

Spring Boot's auto-configuration probably cannot find your entity Dump.

Add the @EntityScan annotation to your application class or a configuration class, for example:

package tech.backend.webapplication;

@SpringBootApplication
@EntityScan("tech.backend.webapplication.com.model")
public class WebapplicationApplication {

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

}
Oliver
  • 1,465
  • 4
  • 17
  • Thanks, but it doesn't work. I got the same Exception – JoHannes Feb 18 '23 at 16:32
  • What version of Spring Boot are you using and what is the full package of the `@EntityScan` class you are using? Do you also have `@EnableJpaRepositories` somewhere? What's the package of the `@Entity` class? – Oliver Feb 18 '23 at 17:04
0

On multi-modules this issue can happen,you can do on your main class,

@SpringBootApplication
@ComponentScan(basePackages = {"tech.backend.webapplication.*"})
public class WebapplicationApplication {

public static void main(String[] args) {
    SpringApplication.run(WebapplicationApplication.class, args);
}
} 
amir-rad
  • 85
  • 7