[英]Spring Boot URL mapping not working when files are present in multiple packages
I tried making a simple REST API using Spring Boot.我尝试使用 Spring 引导制作一个简单的 REST API。 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.
在这种情况下,URL 映射不起作用(使用 Postman 检查),而如果我将它们全部放在与主要 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6 相同的 package 中,则它工作正常
""" Hierarchy of the packages: """ 包的层次结构:
I tried including @componentscan("com.example") in the main class.我尝试在主 class 中包含@componentscan("com.example") 。 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: -- 如果我包含@Componentscan,这是我收到的错误:
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.
moving the main class from com/example/ProductCRUD to com/example将主要 class 从 com/example/ProductCRUD 移动到 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.