[英]Spring Boot URL mapping not working when files are present in multiple packages
我嘗試使用 Spring 引導制作一個簡單的 REST API。 我已將這些類放在單獨的包中。 在這種情況下,URL 映射不起作用(使用 Postman 檢查),而如果我將它們全部放在與主要 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6 相同的 package 中,則它工作正常
""" 包的層次結構:
我嘗試在主 class 中包含@componentscan("com.example") 。 但在這種情況下,它會引發錯誤。
如果有人可以幫助我找出我出錯的地方,那將很有幫助。
在此先感謝您的幫助。
//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);
}
}
-- 如果我包含@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.
將主要 class 從 com/example/ProductCRUD 移動到 com/example
解決您的問題。
差異:
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.