【发布时间】:2021-03-11 17:37:49
【问题描述】:
我尝试使用 Spring Boot 制作一个简单的 REST API。我已将这些类放在单独的包中。在这种情况下,URL 映射不起作用(使用 Postman 检查),而 如果我将它们全部放在与主类相同的包中,它的工作正常。
""" 包的层次结构:
- com.example.ProductCRUD |-----ProductCrudApplication.java(主)
- com.example.ProductCRUD.Controller |-----ProductController.java(控制器)
- com.example.ProductCRUD.Entity |--------Product.java(模型类)
- com.example.Repository |-----ProductRepo.java(Repository 接口)
- com.example.Service |-------ProductService.java(服务类) """
我尝试在主类中包含 @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.
【问题讨论】:
-
尝试用@Repository 注解标记你的仓库。
-
如果将代码签入到github并分享repo链接,将很容易复制和帮助
-
@MohitSharma 我也试过了。没有运气!
标签: java spring spring-boot hibernate spring-mvc