错误服务Rest-Spring启动:java.util.NoSuchElementException:不存在值

错误服务Rest-Spring启动:java.util.NoSuchElementException:不存在值,java,oracle,spring-boot,docker,Java,Oracle,Spring Boot,Docker,早上好,使用spring boot创建REST web服务时,我遇到了一个问题,应用程序向我运行,但是当显示数据时,它向我显示的数据好像ORACLE数据库中不存在该表,我不知道是不是我在类的配置中缺少了一些内容,我附上了我得到的答案: 当我咨询所有客户时: 当我咨询以获取特定记录时: 我的代码 主类 package com.example.consulta; import org.springframework.boot.SpringApplication; import org.spri

早上好,使用spring boot创建REST web服务时,我遇到了一个问题,应用程序向我运行,但是当显示数据时,它向我显示的数据好像ORACLE数据库中不存在该表,我不知道是不是我在类的配置中缺少了一些内容,我附上了我得到的答案:

当我咨询所有客户时:

当我咨询以获取特定记录时:

我的代码

主类

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);
    }
}
控制器

 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);

    }

}
客户服务

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);

}
附件证明我的表与用户cesar一起存在,并且我可以使用SQL Developer进行连接:

创建表的SQL代码:

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)
);
我是spring boot的新手,我正在测试如何使用docker创建服务,我必须将我的数据库放在一个容器中,我不知道它是否会影响

我的项目结构


我遇到了相同的错误,问题出在:

public Cliente findById(int cedula) {
        return clienteDao.findById(cedula).get();
}
我添加了如下验证:

public Cliente findById(int cedula) {
        Optional<Cliente> client = clienteDao.findById(cedula);
        return client.isEmpty() ? null : client.get();
}
findById公共客户(int cedula){
可选客户端=clienteDao.findById(cedula);
返回client.isEmpty()?null:client.get();
}

使用它,只有在找到客户机时才调用get()函数。

简单地说,“@ComponentScan”告诉Spring哪些包中有注释类,这些类应该由Spring管理。因此,例如,如果您有一个用@Controller注释的类,它位于一个包中,而该包没有被Spring扫描,那么您将无法将其用作Spring控制器。请检查此项将@ComponentScan(basePackages={“com.example.consulta.controller”})添加到我的App.java类中,我得到了以下错误:com.example.consulta.controller.clientController中的字段clientService需要找不到类型为“com.example.consulta.service.clientService”的bean。注入点有以下注解:-@ Or.SrpFraskWork.BeANS.Fas.AnNOuto.AutoWordRead(强制= true)动作:考虑在配置中定义COM类型“COM.SCORTA.Service .Cclipse Service”的bean。可以使用类代替接口,可能像我没有使用的那样查看这个!“事务”注释。
<?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>
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
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)
);
public Cliente findById(int cedula) {
        return clienteDao.findById(cedula).get();
}
public Cliente findById(int cedula) {
        Optional<Cliente> client = clienteDao.findById(cedula);
        return client.isEmpty() ? null : client.get();
}