Spring Boot URL mapping not working when files are present in multiple packages



I tried making a simple REST API using Spring Boot. I have put the classes in separate packages. In this case, the URL mapping is not working (checked using Postman), whereas its working fine if I put them all in the same package as that of the main class.

“”” Hierarchy of the packages :

  • com.example.ProductCRUD |—–ProductCrudApplication.java (main)
  • com.example.ProductCRUD.Controller |—–ProductController.java(controller)
  • com.example.ProductCRUD.Entity |——Product.java(model class)
  • com.example.Repository |—–ProductRepo.java(Repository interface)
  • com.example.Service |——-ProductService.java(Service class) “””

I tried including @componentscan(“com.example”) in the main class. But in that case, it throws an error.

If somebody could help me in finding out where I went wrong, it would be helpful.

Thanks in advance for your help.

//PRODUCT MODEL CLASS (Product.java)
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

//MODEL CLASS

@Entity
public class Product {
    @Id
    @GeneratedValue
    private int id;
    private String name;
    private int quantity;
    private int price;
    
    
    
    public Product() {
        super();
        // TODO Auto-generated constructor stub
    }



    public Product(int id, String name, int quantity, int price) {
        super();
        this.id = id;
        this.name = name;
        this.quantity = quantity;
        this.price = price;
    }



    public int getId() {
        return id;
    }



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



    public String getName() {
        return name;
    }



    public void setName(String name) {
        this.name = name;
    }



    public int getQuantity() {
        return quantity;
    }



    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }



    public int getPrice() {
        return price;
    }



    public void setPrice(int price) {
        this.price = price;
    }
    
    
    
}

//Repository (ProductRepo.java) :interface
import org.springframework.data.jpa.repository.JpaRepository;

import com.example.Entity.Product;

public interface ProductRepo extends JpaRepository<Product,Integer> {

    Product findByName(String name);
}
//Service class(ProductService.java)
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.Entity.Product;
import com.example.Repository.ProductRepo;

@Service
public class ProductService {
    @Autowired
    private ProductRepo repo; //service--> repository
    
    //PUT
    public Product create(Product product) {
        Product p=repo.save(product); //default method of JPA to make the data persist in the DB
        return p;
    }
    
    
    public List<Product> createproducts(List<Product> products) {
        List<Product> p=repo.saveAll(products); 
        return p;
        
    }
    
    //GET
    public List<Product> getProducts(){
        List<Product> p=repo.findAll();
        return p;
    }
    
    public Product getProductByid(int id){
        Product p=repo.findById(id).orElse(null); //return id , if id not found then return null
        return p;
    }
    
    public Product getProductByName(String name){
        Product p=repo.findByName(name); //customized method in JPA (declared in the interface)
        return p;
    }
    
    //DELETE
    public String deleteProduct(int id) {
        repo.deleteById(id);
        return "Product removed : "+id;
    }
    
    //UPDATE
    public Product updateProduct(Product product) {
        Product existing=repo.findById(product.getId()).orElse(null);
        existing.setName(product.getName());
        existing.setPrice(product.getPrice());
        existing.setQuantity(product.getQuantity());
        Product p=repo.save(existing);
        return p;
        
    }
    
}

//Controller class (ProductController.java)
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RestController;

import com.example.Entity.Product;
import com.example.Service.ProductService;

@RestController
public class ProductController {
    
    @Autowired
    private ProductService service; //controller --> service
    
    @PostMapping("/create")
    public Product addProduct(@RequestBody Product product) {
        return service.create(product);
    }
    
    @PostMapping("/createProducts")
    public List<Product> addProducts(@RequestBody List<Product> products) {
        return service.createproducts(products);
    }
    
    @GetMapping("/getproducts/{id}")
    public Product getProductById(@PathVariable int id){
        return service.getProductByid(id);
    }
    
    @GetMapping("/getproducts/{name}")
    public Product getProductByName(@PathVariable String name){
        return service.getProductByName(name);
    }
    
    @GetMapping("/getproducts")
    public List<Product> getProducts(){
        return service.getProducts();
    }
    
    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable int id) {
        return service.deleteProduct(id);
    }
    
    @PutMapping("/update/{id}")
    public Product update(@RequestBody Product product) {
        return service.updateProduct(product);
    }
}

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@ComponentScan("com.example")
public class ProductCrudApplication {

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

}

— If I include the @Componentscan this is the error I am receiving :

Field repo in com.example.Service.ProductService required a bean of type 'com.example.Repository.ProductRepo' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.example.Repository.ProductRepo' in your configuration.


Answer

moving the main class from com/example/ProductCRUD to com/example

fixes your issue.

diff:

rename from ProductCRUD/src/main/java/com/example/ProductCRUD/ProductCrudApplication.java
rename to ProductCRUD/src/main/java/com/example/ProductCrudApplication.java
index 1633cbf..93294a2 100644
--- a/ProductCRUD/src/main/java/com/example/ProductCRUD/ProductCrudApplication.java
+++ b/ProductCRUD/src/main/java/com/example/ProductCrudApplication.java
@@ -1,4 +1,4 @@
-package com.example.ProductCRUD;
+package com.example;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;


Source: stackoverflow