Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"status": 404, "error": "Not Found", "path": "/GetProduct"

My controller class is –

package com.javatechie.crud.example.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.javatechie.crud.example.entity.Product;
import com.javatechie.crud.example.service.ProductService;

public class ProductListController {

    @Autowired
    private ProductService service; 
    @GetMapping("/GetProduct")
    public List<Product> addProducts(@RequestBody Product products) throws IOException, ClassNotFoundException, SQLException {      
            System.out.println("Inside addProducts controller method");
        return service.saveProducts(1);
    }   
}

My entity class is –

package com.javatechie.crud.example.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="PRODUCT_TBL")
public class Product {

    @Id
    @GeneratedValue
    private int id;
    private String name;
    private String quantity;
    private double 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 String getQuantity() {
        return quantity;
    }
    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }   
}

JpaRepository implementation is –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

package com.javatechie.crud.example.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="PRODUCT_TBL")
public class Product {

    @Id
    @GeneratedValue
    private int id;
    private String name;
    private String quantity;
    private double 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 String getQuantity() {
        return quantity;
    }
    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }   
}

And the service class is –

package com.javatechie.crud.example.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.javatechie.crud.example.entity.Product;
import com.javatechie.crud.example.repository.ProductRepository;

@Service
public class ProductService {

    @Autowired
    private ProductRepository repository;
    
    public List<Product> saveProducts(int id) {
        return repository.GetRepo(id);
    }   
}

And , the unexpected output which it is giving is –

enter image description here

Database table is as follows –

enter image description here

The output which I was expecting is the json body with sql record having id , name ,price,quantity . How can I achieve this output with native query only ? Please help.

>Solution :

You should add @RestController and @RequestMapping annotations for processing incoming REST requests.

@RestController
@RequestMapping("/api")
public class ProductListController {

    @Autowired
    private ProductService service; 
    @GetMapping("/GetProduct")
    public List<Product> addProducts(@RequestBody Product products) throws IOException, ClassNotFoundException, SQLException {      
        System.out.println("Inside addProducts controller method");
        return service.saveProducts(1);
    }   
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading