spring 404错误提示-页面未找到

lf3rwulv  于 2022-12-10  发布在  Spring
关注(0)|答案(2)|浏览(155)

我正在尝试创建一个Sping Boot 程序,在那里我可以保存企业可能销售的不同产品。我正在将产品保存到MongoDB中。
目前,我正在尝试使用PostMan将产品保存到数据库中。但是,PostMan一直给我这个错误,我想知道我做错了什么(这是我输入PostMan的URL:):

{
    "timestamp": "2022-12-07T22:56:33.866+00:00",
    "status": 404,
    "error": "Not Found",
    "path": "/mdb-spring-boot-product-organizer/api/addProduct"
}

Project Explorer
控制器代码

package com.example.mdbspringbootproductorganizer.controller;

import java.util.List;
import java.util.Optional;

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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.mdbspringbootproductorganizer.model.Product;
import com.example.mdbspringbootproductorganizer.repository.ProductRepository;

@RestController
@RequestMapping("/api")
public class ProductController {
    
    @Autowired
    private ProductRepository repository; 
    
    @PostMapping("/addProduct")
    public String saveProduct(@RequestBody Product product) {
        
        repository.save(product);
        
        return "Added product with id : " + product.getId();
    }
    
    @GetMapping("/findAllProducts")
    public List<Product> getProducts() {
        
        return repository.findAll();
    }
    
    @GetMapping("/findAllProducts/{id}")
    public Optional<Product> getProduct(@PathVariable int id) {
        
        return repository.findById(id);
    }
    
    @DeleteMapping("/delete/{id}")
    public String deleteBook(@PathVariable int id) {
        
        repository.deleteById(id); 
        
        return "Product deleted with id: " + id;
    }
}

资料档案库

package com.example.mdbspringbootproductorganizer.repository;

import org.springframework.data.mongodb.repository.MongoRepository;

import com.example.mdbspringbootproductorganizer.model.Product;

public interface ProductRepository extends MongoRepository<Product, Integer> {

     
}

波约

package com.example.mdbspringbootproductorganizer.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "ProductInventory")
public class Product {
    
    @Id
    private int id;
    
    private String name;
    private double listedPrice; 
    private double purchasePrice; 
    private String condition;
    private String brand; 
    private char shelf;
    private int bin;
    
    public Product(int id, String name, double listedPrice, double purchasePrice, String condition, String brand,
            char shelf, int bin) {
        super();
        this.id = id;
        this.name = name;
        this.listedPrice = listedPrice;
        this.purchasePrice = purchasePrice;
        this.condition = condition;
        this.brand = brand;
        this.shelf = shelf;
        this.bin = bin;
    }

    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 double getListedPrice() {
        return listedPrice;
    }

    public void setListedPrice(double listedPrice) {
        this.listedPrice = listedPrice;
    }

    public double getPurchasePrice() {
        return purchasePrice;
    }

    public void setPurchasePrice(double purchasePrice) {
        this.purchasePrice = purchasePrice;
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public char getShelf() {
        return shelf;
    }

    public void setShelf(char shelf) {
        this.shelf = shelf;
    }

    public int getBin() {
        return bin;
    }

    public void setBin(int bin) {
        this.bin = bin;
    }

    @Override
    public String toString() {
        return "Product [idNumber=" + id + ", name=" + name + ", listedPrice=" + listedPrice + ", purchasePrice="
                + purchasePrice + ", condition=" + condition + ", brand=" + brand + ", shelf=" + shelf + ", bin=" + bin
                + "]";
    } 
                
}

主类

package com.example.mdbspringbootproductorganizer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@SpringBootApplication
@EnableMongoRepositories
public class MdbSpringBootApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(MdbSpringBootApplication.class, args);
    }
    

}````


I have been trying to add different annotations or rename my packages, but none of that has worked.
nwwlzxa7

nwwlzxa71#

我不认为mdb-spring-boot-product-organizer应该是URL的一部分。
试着做一个POSThttp://localhost:8080/api/addProduct

qfe3c7zg

qfe3c7zg2#

尝试将@ComponentScan添加到Sping Boot 应用程序类中。

@SpringBootApplication
@EnableMongoRepositories
@ComponentScan(basePackages={"com.example.mdbspringbootproductorganizer.controller","com.example.mdbspringbootproductorganizer.repository"})
public class MdbSpringBootApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(MdbSpringBootApplication.class, args);
    }
    

}

此错误通常发生在您的spring Boot 应用程序无法找到URL***(因为它是在不同的包中定义的,而不是在具有@SpringBootApplication类的包中定义的)***尽管在Controller中定义!

相关问题