I'm fairly new to spring boot and Java, I've been working on a REST API using springboot for an ecommerce project but for some reason I can't get the products from the database, my products are getting saved to the database but whenever I fetch them the fields show up as null values.this is what I get when I use GET using Postman.
My controller and other files are as follows.
package com.ecommerce.ecommerceappapi.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
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.ecommerce.ecommerceappapi.model.Product;
import com.ecommerce.ecommerceappapi.services.ProductService;
@CrossOrigin(origins = "http://localhost:3000")
@RestController
@RequestMapping("api/v1/")
public class ProductController {
@Autowired
private final ProductService productService;
public ProductController(ProductService productService) {
this.productService = productService;
}
@PostMapping("/products")
public Product createProduct(@RequestBody Product product) {
return productService.createProduct(product);
}
@GetMapping("/products")
public List<Product> getAllProducts() {
return productService.getAllProducts();
}
}
Entity ->
package com.ecommerce.ecommerceappapi.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Entity
@Data
@Table(name="products")
public class ProductEntity {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private long prodId;
private String productName;
private int productPrice;
private String productDesc;
private String productData;
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getProductPrice() {
return productPrice;
}
public void setProductPrice(int productPrice) {
this.productPrice = productPrice;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public String getProductData() {
return productData;
}
public void setProductData(String productData) {
this.productData = productData;
}
public Long getProdId() {
// TODO Auto-generated method stub
return prodId;
}
}
Service Interface ->
package com.ecommerce.ecommerceappapi.services;
import java.util.List;
import com.ecommerce.ecommerceappapi.model.Product;
public interface ProductService {
Product createProduct(Product product);
List<Product> getAllProducts();
}
Service Implementation ->
package com.ecommerce.ecommerceappapi.services;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import com.ecommerce.ecommerceappapi.entity.ProductEntity;
import com.ecommerce.ecommerceappapi.model.Product;
import com.ecommerce.ecommerceappapi.repository.ProductRepository;
@Service
public class ProductServiceImpl implements ProductService {
private ProductRepository productRepository;
public ProductServiceImpl(ProductRepository productRepository) {
super();
this.productRepository = productRepository;
}
@Override
public Product createProduct(Product product) {
// TODO Auto-generated method stub
ProductEntity productEntity = new ProductEntity();
BeanUtils.copyProperties(product, productEntity);
productRepository.save(productEntity);
return product;
}
@Override
public List<Product> getAllProducts(){
List<ProductEntity> productEntities = productRepository.findAll();
List<Product> products = productEntities.stream().map(product -> new Product(
product.getProdId(),
product.getProductName(),
product.getProductPrice(),
product.getProductDesc()))
.collect(Collectors.toList());
return products;
}
}
Repository ->
package com.ecommerce.ecommerceappapi.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.ecommerce.ecommerceappapi.entity.ProductEntity;
@Repository
public interface ProductRepository extends JpaRepository<ProductEntity, Long> {
}
Product ->
package com.ecommerce.ecommerceappapi.model;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
//import java.util.List;
import lombok.Data;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
private long prodId;
private String productName;
private int productPrice;
private String productDesc;
private String productData;
public Product(Long prodId2, String productName2, int productPrice2, String productDesc2) {
// TODO Auto-generated constructor stub;
return;
}
public long getProdId() {
return prodId;
}
public void setProdId(long prodId) {
this.prodId = prodId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getProductPrice() {
return productPrice;
}
public void setProductPrice(int productPrice) {
this.productPrice = productPrice;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public String getProductData() {
return productData;
}
public void setProductData(String productData) {
this.productData = productData;
}
}
I figure the Product class is causing the issue as the method
public Product(Long prodId2, String productName2, int productPrice2, String productDesc2) {
// TODO Auto-generated constructor stub;
return;
}
does not return anything, otherwise I'm totally lost as to what might be the issue here.
CodePudding user response:
I think the problem with your code is in your Product class. In the service you fetch all ProductEntity from the database and for each one you create a new Product object. The problem is when you create the Product object. You call the constructor :
public Product(Long prodId2, String productName2, int productPrice2, String productDesc2) {
// TODO Auto-generated constructor stub;
return;
}
In this constructor you never assign values to attributes of Product class. You should remove this constructor and put this one :
public Product(Long prodId, String productName, int productPrice, String productDesc) {
this.prodId = prodId;
this.productName = productName;
this.productPrice = productPrice;
this.productDesc = productDesc;
}
In the constructor, you should always use this.ATTRIBUT_NAME
to assign values and store them in the class.
CodePudding user response:
I think you should remove the return from the constructor.
and if you have @Data
and @AllArgsConstructor
Lombok annotations you don't have to explicitly generate getters, setters and the constructor.
And I would prefer
@Override
public List<Product> getAllProducts(){
List<ProductEntity> productEntities = productRepository.findAll();
List<Product> products = productEntities.stream()
.map(Product::new)
.collect(Collectors.toList());
return products;
}
And from the Product class will look like this
public Product(ProductEntity productEntity) {
this.prodId = productEntity.getProdId();
this.productName = productEntity.getProductName();
this.productPrice = productEntity.getProductPrice();
this.productDesc = productEntity.ProductDesc();
}