javamysqlspring-bootrestpostman

Getting null response for post, get requests in spring boot


I am new to Spring boot. So I am facing a problem when I am sending get, post requests using Postman. I am getting null response even though everything seems to be the all right.

My controller file is as follows:

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.bind.annotation.*;

import com.javatechie.crud.example.entity.Product;
import com.javatechie.crud.example.service.ProductService;

@RestController
public class ProductController {
    
    @Autowired
    private ProductService service;

    @PostMapping("/addProduct")
    public Product addProduct(@RequestBody Product product) {
        return service.saveProduct(product);
    }

    @PostMapping("/addProducts")
    public List<Product> addProducts(@RequestBody List<Product> products) {
        return service.saveProductAll(products);
    }

    @GetMapping("/products")
    public List<Product> findAllProducts() {
        return service.getProductAll();
    }

    @GetMapping("/productById/{id}")
    public Product findProductById(@PathVariable int id) {
        return service.getProductById(id);
    }

    @GetMapping("/product/{name}")
    public Product findProductByName(@PathVariable String name) {
        return service.getProductByName(name);
    }


    @DeleteMapping("/delete/{id}")
    public String deleteProduct(@PathVariable int id) {
        return service.deleteProduct(id);
    }
}

My entity class file is as follows:

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

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "PRODUCT_TBL")


public class Product {
    
    @Id
    @GeneratedValue
    private int id;
    private String name;
    private int quantity;
    private double price;

}

My service class file is as follows:

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 Product saveProduct(Product product) {
        return repository.save(product);
    }
    
    public List<Product> saveProductAll(List<Product> products) {
        return repository.saveAll(products);
    }
    
    public List<Product> getProductAll() {
        return repository.findAll();
    }
    
    public Product getProductById(int id) {
        return repository.findById(id).orElse(null);
    }

    public Product getProductByName(String name) {
        return repository.findByName(name);
    }
    
    public String deleteProduct(int id) {
        repository.deleteById(id);
        return "Product havig id " +id +"deleted successfully";
    }
}

My application.properties file is:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = india
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
server.port=9191

Edited: Following is my repo class:

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

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

    Product findByName(String name);

}

I am sending the following request for POST via Postman- http://localhost:9191/addProduct

For the above I am getting the following response - {}

For the following GET request also I am getting blank response - http://localhost:9191/products

I am using mysql db having lombok.

Kindly let me know what is going wrong.


Solution

  • This looks like a Lombok plugin installation issue.
    Normally arises when we use IDE's like Eclipse and SpringToolSuite.

    1. It's better if we can manually add getters/setters in our Entity class.

      OR

    2. Install the Lombok plugin in our IDE's by referring the site below.
      Lombok is not generating getter and setter.

    Also, Don't directly pass the result returned by Database in response.
    Kindly use identical DTO classes and return those in Response.
    Refer https://mapstruct.org for such mappings.