'Spring Boot db Docker not returning data H
Good people, look I have a problem with Spring Boot and a database hosted in docker; I really don't know if the problem comes from docker or the code in my database. What happens is that the endpoints seem to work normally and from the Spring side I don't get any errors, when I request information from my database it is where I get an empty json
model User
package com.curso.curso.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;
@Entity
@Table(name = "users")
public class Users {
@Id
@Getter @Setter @Column(name = "id")
private Long id;
@Getter @Setter @Column(name = "name")
private String name;
@Getter @Setter @Column(name = "email")
private String email;
@Getter @Setter @Column(name = "password")
private String password;
@Getter @Setter @Column(name = "telephone")
private String telephone;
}
Controller
package com.curso.curso.controllers;
import java.util.List;
import com.curso.curso.dao.UsersDao;
import com.curso.curso.models.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UsuarioController {
@Autowired
private UsersDao usersDao;
@RequestMapping( value="users", method = RequestMethod.GET )
public List<Users> getusers(){
return usersDao.users();
}
}
UserDao
package com.curso.curso.dao;
import java.util.List;
import com.curso.curso.models.Users;
public interface UsersDao {
List<Users> users();
}
UserDao Implements
package com.curso.curso.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.curso.curso.models.Users;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional
public class UserDaoImp implements UsersDao {
@PersistenceContext
EntityManager entityManager;
@Override
@Transactional
public List<Users> users() {
String query = "SELECT *";
return entityManager.createQuery(query).getResultList();
}
}
Application properties|The database runs on port 49180
server.port:8080
spring.datasource.url=jdbc:mysql://127.0.0.1:49180/test_db?useSSL=false
spring.datasource.dbname=test_db
spring.datasource.username=root
spring.datasource.password=root.password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
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>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.curso</groupId>
<artifactId>curso</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>curso</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
If you could help me I would be very grateful, the truth is I was looking for references about possible errors and I couldn't find anything
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
