简体   繁体   English

在 Springboot 中获取服务 NULL 值的请求 REST API

[英]Get Request serving NULL values in Springboot REST API

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.我对 spring 引导和 Java 相当陌生,我一直在使用 springboot 进行电子商务项目的 REST API 但由于某种原因我无法从数据库中获取产品,我的产品正在保存到数据库中但是每当我获取它们时,字段都会显示为 null 值。 this is what I get when I use GET using Postman.这是我使用 Postman 使用 GET 时得到的结果。

My controller and other files are as follows.我的controller等文件如下。

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我认为产品 class 是导致问题的方法

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.不返回任何东西,否则我完全不知道这里可能是什么问题。

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:我认为您的代码的问题出在您的产品 class 中。在服务中,您从数据库中获取所有 ProductEntity,并为每个创建一个新产品 object。问题出在您创建产品 object 时。您调用构造函数:

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:在此构造函数中,您永远不会为 Product class 的属性赋值。您应该删除此构造函数并将其放入:

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. ATTRIBUT_NAME赋值并存储在 class 中。

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.如果您有@Data@AllArgsConstructor Lombok 注释,则不必显式生成 getter、setter 和构造函数。

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从产品 class 看起来像这样

public Product(ProductEntity productEntity) {
        this.prodId = productEntity.getProdId();
        this.productName = productEntity.getProductName();
        this.productPrice = productEntity.getProductPrice();
        this.productDesc = productEntity.ProductDesc();
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM