I am creating a spring boot application, wherein any client can submit the request, these request can be GET, PUT, POST, DELETE.

I'm getting the below error and I'm able not to fix it after soo many changes as well.

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-01-13T01:58:33.595-05:00 ERROR 10400 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.example.sqch8ex5.ProductController required a bean of type 'service.ProductService' that could not be found.


Action:

Consider defining a bean of type 'service.ProductService' in your configuration.

Here is the project structure - Project structure for more reference

Below are all the files -

SqCh8Ex5Application.java

package com.example.sqch8ex5;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class SqCh8Ex5Application {

    public static void main(String[] args) {
        SpringApplication.run(SqCh8Ex5Application.class, args);
    }

}

ProductController.java

package com.example.sqch8ex5;

import model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import service.ProductService;

@Controller
public class ProductController {

    private final ProductService productService;

    // We use DI through the controller’s constructor
    // parameters to get the service bean from the
    // Spring context

    @Autowired
    public ProductController(ProductService productService){
        this.productService = productService;
    }

    @GetMapping("/products")
    public String viewProducts(Model model){
        var products = productService.findAll();
        model.addAttribute("products", products);
        return "products.html";
    }

    @PostMapping("/products")
    public String addProduct(@RequestParam String name,
                             @RequestParam double price,
                             Model model){
        Product p = new Product();
        p.setName(name);
        p.setPrice(price);
        productService.addProduct(p);

        var products = productService.findAll();
        model.addAttribute("products",products);
        return "products.html";
    }
}

products.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Home Page</title>
</head>
<body>
  <h1>Products</h1>
  <h2>View products</h2>
  <table>
    <tr>
      <th>PRODUCT NAME</th>
      <th>PRODUCT PRICE</th>
    </tr>
    <tr th:each="p:${products}">
      <td th:text="${p.name}"></td>
      <td th:text="${p.price}"></td>
    </tr>
  </table>

  <h2>Add a product</h2>
<form action="/products" method="post">
  Name:<input type="text" name="name"><br />
  Price:<input type="number" step="any" name ="price"><br />
  <buttom type="submit">Add product</buttom>
</form>
</body>
</html>

ProductService.java

package service;

import model.Product;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductService {

    private List<Product> products = new ArrayList<>();

    public ProductService() {
    }

    public void addProduct(Product p){
        products.add(p);
    }

    public List<Product> findAll(){
        return products;
    }
}

I'm just starting with spring now. So, do help me with some sort off solution.

I was expecting to see a view of the html page on the browser (aka http://localhost:8080/products)

0

There are 0 best solutions below