Cesar Justo
Cesar Justo

Reputation: 803

Spring boot failed when trying to build a Rest service

Good afternoon I am trying to generate a Rest service with java spring boot, but when trying to execute the jar I get the following error, which is giving me a headache and I don't know what it is specifically:

java.lang.NoSuchMethodError: org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource.(Lorg/springframework/core/type/AnnotationMetadata;Ljava/lang/Class;Lorg/springframework/core/io/ResourceLoader;Lorg/springframework/core/env/Environment;)V at org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport$1.(AbstractRepositoryConfigurationSourceSupport.java:67) at org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport.getConfigurationSource(AbstractRepositoryConfigurationSourceSupport.java:66) at org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport.registerBeanDefinitions(AbstractRepositoryConfigurationSourceSupport.java:58) at org.springframework.context.annotation.ImportBeanDefinitionRegistrar.registerBeanDefinitions(ImportBeanDefinitionRegistrar.java:86) at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.lambda$loadBeanDefinitionsFromRegistrars$1(ConfigurationClassBeanDefinitionReader.java:385) at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromRegistrars(ConfigurationClassBeanDefinitionReader.java:384) at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:148) at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:120) at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331) at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:236) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:347) at org.springframework.boot.SpringApplication.run(SpringApplication.java:295) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1112) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1101) at com.example.consulta.SpringBootSoapApp.main(SpringBootSoapApp.java:12) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

java.lang.NoSuchFieldException: findAnnotationCache at java.lang.Class.getDeclaredField(Class.java:2070) at org.springframework.boot.devtools.restart.Restarter.clear(Restarter.java:376) at org.springframework.boot.devtools.restart.Restarter.cleanupKnownCaches(Restarter.java:361) at org.springframework.boot.devtools.restart.Restarter.cleanupCaches(Restarter.java:348) at org.springframework.boot.devtools.restart.Restarter.access$000(Restarter.java:81) at org.springframework.boot.devtools.restart.Restarter$1.call(Restarter.java:170) at org.springframework.boot.devtools.restart.Restarter$1.call(Restarter.java:165) at org.springframework.boot.devtools.restart.Restarter$LeakSafeThread.run(Restarter.java:602)

Annex my code

package com.example.consulta;

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

main.class

@SpringBootApplication
public class SpringBootSoapApp {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootSoapApp.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>{
}

Entity

package com.example.consulta.entity;

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

@Entity
@Table(name="Cliente")
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;
    }   

}

Service

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

}

ServiceImpl

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;


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

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


    }

pom.xml

  <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example.consulta</groupId>
  <artifactId>ServiceConsultaCliente</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>ServiceConsultaCliente</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>8</java.version>
  </properties>

  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.2.4.RELEASE</version>
    </dependency>       
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>2.2.4.RELEASE</version>
    </dependency>
    <dependency>
         <groupId>com.oracle.jdbc</groupId>
         <artifactId>ojdbc8</artifactId>
         <version>12.2.0.1</version>
     </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <version>1.3.0.RELEASE</version>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
          <id>codelds</id>
           <url>https://code.lds.org/nexus/content/groups/main-repo</url>
          </repository>
    </repositories>   
</project>

application.properties

# Oracle settings

    spring.application.name=prueba-microservicio-cliente
    server.port=8001
    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

enter image description here

UPDATE After doing what specified by Marcos Barrero I get the following error: error i get:

pom.xml (final)

<?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>

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

enter image description here

Upvotes: 0

Views: 1034

Answers (4)

When you face NoSuchMethodException at runtime, it generally means 'classpath hell' - e.g., for simplification, when you've got two version of the same library (one containing the method, but the other - not) and only god knows which version will actually get into your final build if you don't do anything.

To find such dependencies, you shoult run 'mvn dependency:tree' command and search for something related to 'org.springframework.boot.devtools'... => most probably you'll see two versions

Then - you should either use section of maven POM to strictly set the version, or, as guys suggested above - use spring boot parent (also, a more advanced solution is importing a BOM - which is kind of a set of rules for dependency versions).

Upvotes: 0

Marcos Barbero
Marcos Barbero

Reputation: 1119

From what I can see your problem is the way you're importing the dependencies, the recommended way to do so is by using the Spring Boot's parent pom and its managed dependencies.

I recommend you to use start.spring.io to generate your next projects, here's a sample with the Spring Boot dependencies that you have, the major diff is the parent pom.

For your project, you need to add the extra dependencies such as oracle and any further configuration you have.

<?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.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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>

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

UPDATE

Here's your version with h2 (you have to change it to oracle)

<?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>

Upvotes: 1

Samuel
Samuel

Reputation: 1149

Try use this pom.xml

It was generated by https://start.spring.io/ and I inserted your Oracle dependencies and repositories.

<?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.4.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </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>

    <dependency>
        <groupId>com.oracle.jdbc</groupId>
        <artifactId>ojdbc8</artifactId>
        <version>12.2.0.1</version>
    </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>
<repositories>
    <repository>
        <id>codelds</id>
        <url>https://code.lds.org/nexus/content/groups/main-repo</url>
    </repository>
</repositories>

Upvotes: 1

Anton Shelenkov
Anton Shelenkov

Reputation: 325

Try to add spring boot parent pom. With it you shouldn't specify starters versions - parent pom contains all information about needed versions.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
</parent>

Upvotes: 2

Related Questions