Home > database >  Spring Boot Postman 404 and Not Found Error
Spring Boot Postman 404 and Not Found Error

Time:12-08

I am trying to create a Spring Boot program where I can save different products that a business may have for sale. I am saving the products into MongoDB.

Currently, I am trying to use PostMan to save a product into the database. However, PostMan keeps giving me this error, and I was wondering what I am doing wrong (this is the URL I am inputting into PostMan: "http://localhost:8080/mdb-spring-boot-product-organizer/api/addProduct"):


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

Project Explorer

Controller Code


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;
    }
}

Repository


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> {

     
}

POJO


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
                  "]";
    } 
                
}

Main class

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.

CodePudding user response:

I don't think mdb-spring-boot-product-organizer should be part of the URL.

Try making a POST to http://localhost:8080/api/addProduct.

CodePudding user response:

Try adding @ComponentScan to your Spring boot application class.

@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);
    }
    

}

This error comes usually when your spring boot application cannot find the URL (since it is defined in different package than one with @SpringBootApplication class) despite defined in Controller!

  • Related