3

Good morning, I have a problem when making a REST web service with spring boot, the application runs to me, but when presenting the data it shows me as if the table did not exist in the database ORACLE, I do not know if it is that I am missing something in the configuration of the classes, a continuation I attach the answers that I get:

When I consult to get all the clients: enter image description here

When I consult to obtain a particular record:

enter image description here

My Code

Main Class

package com.example.consulta;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

Controller

 package com.example.consulta.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.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.example.consulta.entity.Cliente;
import com.example.consulta.service.ClienteService;

@RestController
public class ClienteController {

    @Autowired
    private ClienteService  clienteService;

    @GetMapping("/allCliente")
    public List<Cliente> listarCliente(){
        return clienteService.findAll();
    }

    @GetMapping("/cliente/{cedula}")
    public Cliente detalleCliente(@PathVariable int cedula) {
        return  clienteService.findById(cedula);

    }

}

DAO

package com.example.consulta.DAO;
import org.springframework.data.repository.CrudRepository;

import com.example.consulta.entity.Cliente;

public interface ClienteDAO extends CrudRepository<Cliente, Integer>{
}

Cliente

package com.example.consulta.entity;

import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class Cliente {

    @Id
    private int cedula;
    private String nombre;
    private String apellido;
    private String tipo_cliente;

    public int getCedula() {
        return cedula;
    }
    public void setCedula(int cedula) {
        this.cedula = cedula;
    }
    public String getNombre() {
        return nombre;
    }
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
    public String getApellido() {
        return apellido;
    }
    public void setApellido(String apellido) {
        this.apellido = apellido;
    }
    public String getTipo_cliente() {
        return tipo_cliente;
    }
    public void setTipo_cliente(String tipo_cliente) {
        this.tipo_cliente = tipo_cliente;
    }   

}

clienteService

package com.example.consulta.service;

import java.util.List;

import com.example.consulta.entity.Cliente;

public interface ClienteService {

    public List<Cliente> findAll();
    public Cliente findById(int cedula);

}

ClienteServiceImpl

package com.example.consulta.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.consulta.DAO.ClienteDAO;
import com.example.consulta.entity.Cliente;

@Service

    public class ClienteServiceImpl implements ClienteService{

        @Autowired
        private ClienteDAO clienteDao;

        @Override
        @Transactional
        public List<Cliente> findAll() {
            return (List<Cliente>) clienteDao.findAll();
        }

        @Override
        public Cliente findById(int cedula) {
            return clienteDao.findById(cedula).get();
        }


    }

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.2.1.RELEASE</version>
    </parent>
    <groupId>com.example.consulta</groupId>
    <artifactId>ServiceConsultaCliente</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ServiceConsultaCliente</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</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>

        <!-- change it to oracle driver -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.properties

spring.application.name=prueba-microservicio-cliente
spring.datasource.url=jdbc:oracle:thin:@//10.164.7.203:1521/ORCLPDB1.localdomain
spring.datasource.username=cesar
spring.datasource.password=xxxxx123
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.hibernate.ddl-auto=update

Annex proof that my table exists with the user cesar, and I can connect using SQL Developer:

enter image description here enter image description here enter image description here

SQL code to create the table:

CREATE TABLE cliente(
        cedula NUMBER(10) NOT NULL,
        nombre varchar (15) NOT NULL,
        apellido varchar (15),
        tipo_cliente varchar(10) NOT NULL,
        PRIMARY KEY (cedula)
);

I am new to spring boot, and I am testing to create microvservices with docker, I have to have my database in a container, which I don't know if it influences

Structure of my project

enter image description here

Cesar Justo
  • 707
  • 2
  • 11
  • 35
  • Simply put - '@ComponentScan' tells Spring in which packages you have annotated classes which should be managed by Spring. So, for example, if you have a class annotated with @Controller which is in a package which is not scanned by Spring, you will not be able to use it as Spring controller. please check this https://stackoverflow.com/questions/28963639/how-to-understand-spring-componentscan – Sunil Kumar Feb 11 '20 at 14:31
  • 1
    Add the @ComponentScan (basePackages = {"com.example.consulta.controller"}) to my App.java class and I got the following error: Field clientService in com.example.consulta.controller.ClienteController requires a bean of type 'com.example.consulta.service.ClienteService' that could not be found. The injection point has the following annotations: - @ org.springframework.beans.factory.annotation.Autowired (mandatory = true) Action: Consider defining a bean of type 'com.example.consulta.service.ClienteService' in your configuration. – Cesar Justo Feb 11 '20 at 14:46
  • can you use class in place of interface, probably look at this as i have not used !@Transactional' annotations. https://stackoverflow.com/questions/2387431/spring-autowiring-class-vs-interface – Sunil Kumar Feb 11 '20 at 15:22

1 Answers1

1

I was passing through the same error and the problem is in the:

public Cliente findById(int cedula) {
        return clienteDao.findById(cedula).get();
}

I added a validation like this:

public Cliente findById(int cedula) {
        Optional<Cliente> client = clienteDao.findById(cedula);
        return client.isEmpty() ? null : client.get();
}

And with it, you call the get() function only if the Cliente was found.