Issue: API Returning Empty JSON Objects {}

facing an issue in my Spring Boot project. My API for fetching products is returning empty objects: [{}, {}, {}, {}] But I have verified that data exists in my H2 database.
47 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @Shruti! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
ayylmao123xdd
ayylmao123xdd2mo ago
hi can you show the code the service one
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
YoChrisRapNow
YoChrisRapNow2mo ago
package com.example.EcomTutDemo11.service; import com.example.EcomTutDemo11.model.Product; import com.example.EcomTutDemo11.repo.ProductRepo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service
public class ProdcutService {
@Autowired
private ProductRepo repo;

public List<Product> getAllProducts(){

return repo.findAll();
}

}
public class ProdcutService {
@Autowired
private ProductRepo repo;

public List<Product> getAllProducts(){

return repo.findAll();
}

}
This message has been formatted automatically. You can disable this using /preferences.
Peter Rader
Peter Rader2mo ago
Have you considered using data-rest ( https://spring.io/projects/spring-data-rest )?
Spring Data REST
Spring Data REST
Level up your Java code and explore what Spring can do for you.
ayylmao123xdd
ayylmao123xdd2mo ago
show the product repo and the controller also shos the product class show
Shruti
ShrutiOP2mo ago
product repo
YoChrisRapNow
YoChrisRapNow2mo ago
package com.example.EcomTutDemo11.repo; import com.example.EcomTutDemo11.model.Product; import jakarta.persistence.criteria.CriteriaBuilder; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository
public interface ProductRepo extends JpaRepository<Product, Integer> {

}
public interface ProductRepo extends JpaRepository<Product, Integer> {

}
This message has been formatted automatically. You can disable this using /preferences.
YoChrisRapNow
YoChrisRapNow2mo ago
service package com.example.EcomTutDemo11.service; import com.example.EcomTutDemo11.model.Product; import com.example.EcomTutDemo11.repo.ProductRepo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service
public class ProdcutService {
@Autowired
private ProductRepo repo;

public List<Product> getAllProducts(){

return repo.findAll();
}

}
public class ProdcutService {
@Autowired
private ProductRepo repo;

public List<Product> getAllProducts(){

return repo.findAll();
}

}
This message has been formatted automatically. You can disable this using /preferences.
YoChrisRapNow
YoChrisRapNow2mo ago
model package com.example.EcomTutDemo11.model; import jakarta.persistence.Entity; import jakarta.persistence.Id; import lombok.*; import java.math.BigDecimal; import java.util.Date; @Data @AllArgsConstructor @NoArgsConstructor @Getter @Setter @Entity
public class Product {
@Id
private int id;
private String name;
private String description;
private String brand;
private BigDecimal price;
private String category;
private Date releaseDate;
private boolean available;
private int quantity;


}
public class Product {
@Id
private int id;
private String name;
private String description;
private String brand;
private BigDecimal price;
private String category;
private Date releaseDate;
private boolean available;
private int quantity;


}
This message has been formatted automatically. You can disable this using /preferences.
YoChrisRapNow
YoChrisRapNow2mo ago
controller package com.example.EcomTutDemo11.controller; import com.example.EcomTutDemo11.model.Product; import com.example.EcomTutDemo11.service.ProdcutService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/api")
public class Prodcontroller {

@Autowired
private ProdcutService service;
@GetMapping("/")
public String g()
{
return "hi";
}

@GetMapping("/products")
public List<Product> getAllProducts()
{
List<Product> products = service.getAllProducts();
System.out.println(" API /products was called!");
System.out.println("Fetched Products: " + products);
return products;
}
public class Prodcontroller {

@Autowired
private ProdcutService service;
@GetMapping("/")
public String g()
{
return "hi";
}

@GetMapping("/products")
public List<Product> getAllProducts()
{
List<Product> products = service.getAllProducts();
System.out.println(" API /products was called!");
System.out.println("Fetched Products: " + products);
return products;
}
This message has been formatted automatically. You can disable this using /preferences.
ayylmao123xdd
ayylmao123xdd2mo ago
can you try using any other database than h2 maybe the database itself is the problem beacuse the code looks good oh
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
try adding @Shruti add the generated value annotation to your id and see if it helps
Shruti
ShrutiOP2mo ago
i am using h2 only i added this @GeneratedValue(strategy = GenerationType.IDENTITY) but no difference
ayylmao123xdd
ayylmao123xdd2mo ago
h2 database isnt good as a real one its good just for testing @dan1st can you take a look h2 doesnt return objects
dan1st
dan1st2mo ago
Can I see the DB configuration (application.properties) and the code inserting objects? If it is an automated test,I'd like to see that as well If applicable, it would be useful to see the content of the relevant table just because SO is down after they've migrated to GCP yesterday?
Shruti
ShrutiOP2mo ago
application properties spring.application.name=EcomTutDemo11 server.port=9092 spring.datasource.url=jdbc:h2:mem:ecom sspring.datasource.driver-class-name=org.h2.Driver spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.defer-datasource-initialization=true data.sql INSERT INTO Product (id, name, description, brand, price, category, release_date, available, quantity) VALUES (1, 'Smartphone', 'Latest model with high performance', 'Samsung', 69999.99, 'Electronics', '2025-04-01', true, 50), (2, 'Laptop', 'Ultra-fast gaming laptop', 'Asus', 129999.99, 'Computers', '2025-03-15', true, 20), (3, 'Headphones', 'Noise-cancelling wireless headphones', 'Sony', 14999.50, 'Accessories', '2025-02-20', true, 100), (4, 'Smartwatch', 'Feature-rich smartwatch with health tracking', 'Apple', 39999.00, 'Wearables', '2025-01-10', true, 30);
Shruti
ShrutiOP2mo ago
No description
Shruti
ShrutiOP2mo ago
No description
JavaBot
JavaBot2mo ago
application properties spring.application.name=EcomTutDemo11
server.port=9092

spring.datasource.url=jdbc:h2:mem:ecom
sspring.datasource.driver-class-name=org.h2.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

spring.jpa.defer-datasource-initialization=true
application properties spring.application.name=EcomTutDemo11
server.port=9092

spring.datasource.url=jdbc:h2:mem:ecom
sspring.datasource.driver-class-name=org.h2.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

spring.jpa.defer-datasource-initialization=true
data.sql INSERT INTO Product (id, name, description, brand, price, category, release_date, available, quantity) VALUES
(1, 'Smartphone', 'Latest model with high performance', 'Samsung', 69999.99, 'Electronics', '2025-04-01', true, 50),
(2, 'Laptop', 'Ultra-fast gaming laptop', 'Asus', 129999.99, 'Computers', '2025-03-15', true, 20),
(3, 'Headphones', 'Noise-cancelling wireless headphones', 'Sony', 14999.50, 'Accessories', '2025-02-20', true, 100),
(4, 'Smartwatch', 'Feature-rich smartwatch with health tracking', 'Apple', 39999.00, 'Wearables', '2025-01-10', true, 30);
data.sql INSERT INTO Product (id, name, description, brand, price, category, release_date, available, quantity) VALUES
(1, 'Smartphone', 'Latest model with high performance', 'Samsung', 69999.99, 'Electronics', '2025-04-01', true, 50),
(2, 'Laptop', 'Ultra-fast gaming laptop', 'Asus', 129999.99, 'Computers', '2025-03-15', true, 20),
(3, 'Headphones', 'Noise-cancelling wireless headphones', 'Sony', 14999.50, 'Accessories', '2025-02-20', true, 100),
(4, 'Smartwatch', 'Feature-rich smartwatch with health tracking', 'Apple', 39999.00, 'Wearables', '2025-01-10', true, 30);
dan1st
dan1st2mo ago
oh so you are getting the entries but cannot find anything there wait, is there lombok? yep, it's because of lombok I'd show you my own SO answer but SO is down rn First update to the latest lombok version and then add <proc>full</proc> to the <configuration> of the maven-compiler-plugin @Shruti or don't use lombok if you don't want your application to break lol
ayylmao123xdd
ayylmao123xdd2mo ago
yes lol
dan1st
dan1st2mo ago
it's really annoying because I have written a good Stack Overflow answer explaining the issue
ayylmao123xdd
ayylmao123xdd2mo ago
extended april fools joke
dan1st
dan1st2mo ago
they migrated their servers to Google Cloud yesterday so take it as you want
ayylmao123xdd
ayylmao123xdd2mo ago
if gpt wasnt a thing all engineers would be doomed
dan1st
dan1st2mo ago
though it seems that the other SE sites got issues as well
Shruti
ShrutiOP2mo ago
Yeah I used lombok.. previously it was giving mvn not found then I reinstalled everything..then it gave jdk not found because it was pointing to older version..I resolved that and now this ..
dan1st
dan1st2mo ago
for old JDK versions, it "works" for newer JDK versions, you need to change the things I mentioned though there are alternatives to <proc>full</proc>
dan1st
dan1st2mo ago
Stack Overflow
Build Failure: @Data Annotation won't regonize by Maven
I consider that my maven has problems with the annotation @Data from lombok. About the project: Java 23 Spring Boot Version 3.3.4 also using Intellij ultimate The error appears after like mvn comp...
ayylmao123xdd
ayylmao123xdd2mo ago
is this tag the replacement for the annotation processor tag in older jdks
dan1st
dan1st2mo ago
no
ayylmao123xdd
ayylmao123xdd2mo ago
or does it have some extra oh ok
dan1st
dan1st2mo ago
you can use the annotation processor tag instead -proc:full pretty much means "compile and register annotation processors automatically"
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Shruti
ShrutiOP2mo ago
same issue
No description
No description
YoChrisRapNow
YoChrisRapNow2mo ago
i added this @Override
public String toString() {
return "Product{id=" + id + ", name='" + name + "', brand='" + brand + "', price=" + price + ", category='" + category + "', quantity=" + quantity + "}";
} and got results
public String toString() {
return "Product{id=" + id + ", name='" + name + "', brand='" + brand + "', price=" + price + ", category='" + category + "', quantity=" + quantity + "}";
} and got results
This message has been formatted automatically. You can disable this using /preferences.
Shruti
ShrutiOP2mo ago
thank you so much
JavaBot
JavaBot2mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
straightface
straightface2mo ago
@Shruti upload whole pom
Shruti
ShrutiOP2mo ago
straightface
straightface2mo ago
do you have this enabled? @Shruti
Shruti
ShrutiOP2mo ago
yes i did that already
straightface
straightface2mo ago
Can you try with java 11?
ayylmao123xdd
ayylmao123xdd2mo ago
he said it works already
straightface
straightface2mo ago
Ahh I missed it
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?