0

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.

RDX
  • 145
  • 3
  • 12
  • It seems there is a typo in the url for get request and is there any error you are getting in the logs? – GD07 Aug 19 '23 at 06:13
  • hi @GD07, sorry while typing in the post the typo came. Actually it should be "products" and for this it is giving blank output having the response code 200 OK. – RDX Aug 19 '23 at 06:27
  • Is any data already stored in the database with POST requests? – Karsten Aug 19 '23 at 06:53
  • Hi @Karsten, no that is not happening. – RDX Aug 19 '23 at 06:57
  • This is strange. The fact that you get a response in case of your POST requests tells that anything is happening already and does look fine for framework. Next thing to check is if data receives at your code using a debugger. – Karsten Aug 19 '23 at 07:08
  • hi @RDX, Can you try to manually save a record in Database and try the /get API Also, if you can tell which editor are you using ? Eclipse has some issue with Lombok. Try generating getters/setters and give it a try – Ajinkya Karode Aug 19 '23 at 07:13
  • Can you show the repo class? There might be some issue there – hermit Aug 19 '23 at 07:34
  • @hermit, I have added the repo class now. Please let me know if you can find anything. – RDX Aug 19 '23 at 07:59
  • Are you sure you are properly sending the request body via Postman? Can you upload screenshot of your postman? – hermit Aug 19 '23 at 08:06
  • hi @RDX Did you try manually adding getters and setters ? – Ajinkya Karode Aug 19 '23 at 09:53
  • hi @AjinkyaKarode, yes using getters and setters it resolved. Thanks for the info. So I understand using Eclipse we can't use lombok, right? – RDX Aug 19 '23 at 11:48
  • you can use eclipse with Lombok, but need to manually install it. Refer the answer below and upvote. Thank you – Ajinkya Karode Aug 19 '23 at 12:05

1 Answers1

1

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.

Ajinkya Karode
  • 157
  • 1
  • 12