0

I've been working lately on a Spring Boot project that updates and fetches Data from a MySQL Database in JSON format, but when I run my application I have an error page:

[screenshot of the error on my browser][1] [1]: https://i.sstatic.net/CkYmr.png

My Entity Class is:

package com.project.project.entities;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

@Entity
@Table(name = "products")
// why serializable ?? every entity in JPA is automatically-serializable,  connection between different networks
public class Product implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) // assign a unique value to your identity field automatically
    private Long id;
    private String designation;
    private int price;
    private int quantity;
    private int category_id;

    // the owning side of the relationship, side of the foreign key

    @ManyToOne(fetch = FetchType.LAZY )// many products to one category
    @JoinColumn(name = "category_id" , insertable = false , updatable = false)
    // means that the product table will have a fk_column named...
    private Category category;


    // categoryId foreign key referencing to the primary key on Category
    // Double and Integer in case both variables are unknown -> Category constructor
    public Product(Long id, String designation, Integer price, Integer quantity, int categor_id) {
        this.id = id;
        this.designation = designation;
        this.price = price;
        this.quantity = quantity;
        this.category_id = category_id;
    }

My Repository:

import com.project.project.entities.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;


@Repository
public interface ProductRepository extends JpaRepository<Product,Long> {

}

My Service component:

import com.project.project.dao.ProductRepository;
import com.project.project.entities.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class ProductService {

    @Autowired // establish injection
    private ProductRepository productRepository;

    // GET-ALL
    public List<Product> getAllProducts() {
        List<Product> products = new ArrayList<>();
        productRepository.findAll().forEach(products::add);
        return products;
    }

    // GET
    public Product getProduct(Long id) {
        return productRepository.getOne(id);
    }

    // POST
    public void addProduct(Product product){
        productRepository.save(product);
    }

    // PUT
    public void updateProduct(Long id, Product product) {
        product.setId(id);
        productRepository.save(product);
    }
    // DELETE
    public void deleteProduct(Long id){
        productRepository.deleteById(id);
    }
}

My REST Controller is :

import com.project.project.entities.Product;
import com.project.project.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class ProductRestController {

    @Autowired
    ProductService productService;

   /* @Autowired // establish injection
    public ProductRepository productRepository;*/

    // GET-ALL
    @GetMapping(value = "/all")
    public @ResponseBody List<Product> getAllProducts() {
        return productService.getAllProducts();
    }


    /*@GetMapping(value = "/products/{id}")
    @ResponseBody
    public List<Product> getProducts(@RequestParam String designation) {
        return productRepository.findByDesignation(designation);
    }*/

    // GET
    @GetMapping(value = "/products/{id}")
    public Product getProduct(@PathVariable(name = "id") Long id) {
        return productService.getProduct(id);
    }

    // PUT
    @PutMapping(value = "/products/{id}")
    public void updateProduct(@PathVariable(name = "id") Long id, @RequestBody Product product) {
        productService.updateProduct(id, product);
    }

    // POST
    @PostMapping(value = "/products")
    public void save(@RequestBody Product product) {
        productService.addProduct(product);
    }

    // DELETE
    @DeleteMapping(value = "/products/{id}")
    public void delete(@PathVariable(name = "id") Long id) {
        productService.deleteProduct(id);
    }
}

My application.properties:

spring.datasource.url = jdbc:mysql://localhost:3306/sport_shop?serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = emisql
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
server.port = 9091
idea.spring.boot.filter.autoconfig=false 
spring.jpa.properties.hibernate.jdbc.time_zone=UTC

And my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.project</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>project</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Where do you guys think the problem issued from ?

4
  • 2
    please attach application logs - this error page tells nothing Commented Aug 1, 2019 at 16:48
  • The stack trace on your tomcat instance should tell you where the issue is. Commented Aug 1, 2019 at 16:48
  • I cannot see @RestController annotation on your controller. As per screenshot it was unable to find the api path you are trying to hit. 404 denotes path not found Commented Aug 1, 2019 at 16:50
  • That solved the issue @RaviTeja, thanks !!! Commented Aug 2, 2019 at 0:26

1 Answer 1

1

Just set the @Controller as annotation on your controller.

//imports

@Controller
public class ProductRestController {

    @Autowired
    ProductService productService;

    //GetMapping etc


}

You can use the @RestController annotation as well. Read more about the differences here:

Difference between spring @Controller and @RestController annotation

Sign up to request clarification or add additional context in comments.

2 Comments

This solved the issue @theshadog , plus I needed to add a default constructor that was missing in my Entity Class. Thanks for the help !!
You can use annotations for that as well: projectlombok.org/features/constructor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.