When I am trying to access data from my MySQL DB codedecode using HTTP Get Method either on browser client or postman client there is empty curly braces in array [{}] is shown. But, I have data into my database. I have attached all my code including the dependencies of my Pom.xml and and my application props for my project.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.codedecode</groupId>
<artifactId>CodeDecodeCRUD</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CodeDecodeCRUD</name>
<description>Demo project for Spring Boot and Hibernate</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application controller
package com.codedecode.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CodeDecodeCrudApplication {
public static void main(String[] args) {
SpringApplication.run(CodeDecodeCrudApplication.class, args);
}
}
Employee Rest Controller
package com.codedecode.demo.controller;
import java.util.List;
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 com.codedecode.demo.entity.Employee;
import com.codedecode.demo.repos.EmployeeCrudRepo;
@RestController
@RequestMapping("/code")
public class EmployeeController {
@Autowired
EmployeeCrudRepo empCrud;
@GetMapping("/getall")
public List<Employee> getAllEmployees() {
System.out.println("getting all employees");
return empCrud.findAll();
}
}
Employee Entity
package com.codedecode.demo.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;
@Entity
@Table(name = "emp")
@Data
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
}
Employee CRUD Repository
package com.codedecode.demo.repos;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.codedecode.demo.entity.Employee;
@Repository
public interface EmployeeCrudRepo extends JpaRepository<Employee, Long>{
}
application.properties
#server.port=9090
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/codedecode?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=create
#spring.jpa.show-sql=true
#spring.jpa.properties.hibernate.format_sql=true
I am using, MySQL v8.0.33, Spring v3.1.1, Java v17, STS v4, lombok v1.18.28
I tried checking the cofiguration of my MySQL DB, but everything working fine. Tables are created when the server runs.
I tried rechecking my code and all the dependencies but unable to find any issue.
Please help me in this issue as this is my first time I am asking question on Stack Overflow! Thank You
2
Answers
I think there is some issue in STS v4 and Spring v3.1.1 due to which I am facing this problem, as I tried this same project on Eclipse with Spring v2.7.13 and it runs flawlessly. Either way, my issue is resolved for now using Eclipse but if anyone got the solution for my issue on the STS v4 and Spring v3.1.1 version, please let me know I am eagerly waiting for the solution.
I am also trying my best to get the solution on my own, but I am using this as an opportunity to connect with the best brains of this lovely community of coders for the best solution. Thank you and happy coding.
Here are a few things you can check to troubleshoot the issue:
Database Connectivity:
codedecode
) exists and is accessible with the provided credentials (root/root
).emp
table in thecodedecode
database and ensure it contains data.Entity Mapping:
Employee
entity class (com.codedecode.demo.entity.Employee
) is correctly mapped to theemp
table in the database.id
,name
).@Entity
,@Table
, and@Data
annotations are imported from the correct packages (jakarta.persistence.*
andlombok.*
).Repository Configuration:
EmployeeCrudRepo
interface (com.codedecode.demo.repos.EmployeeCrudRepo
) is correctly extendingJpaRepository
.Application Configuration:
application.properties
file is located in the correct directory (src/main/resources
) and is being picked up by the application.mysql-connector-java
) is being resolved correctly by Maven and included in the classpath.spring.datasource.url
,spring.datasource.username
, andspring.datasource.password
properties are correctly configured to connect to your MySQL server.Logging and Debugging:
application.properties
:By reviewing these aspects, you should be able to identify the issue causing the empty JSON response. Additionally, examining the application logs and any error messages can provide valuable insights into the problem.