首页 > 解决方案 > Spring Boot 应用程序运行失败,即使回滚到最后一次成功的构建提交

问题描述

这是我在这里的第一个问题,所以请善待......

我有一个使用 gradle 运行的 SpringBoot REST API。我最近在我的 build.gradle 中添加了一个新依赖项(实现组:'com.smsfactor',名称:'smsfactor',版本:'1.0'),编写了一些代码,当我尝试运行应用程序时,我有了这个错误 :

Error occurred during initialization of boot layer
java.lang.module.ResolutionException: Module lombok does not read a module that exports org.mapstruct.ap.spi

非常令人惊讶,因为我没有对 lombok 库进行任何新的使用。我做的第一件事是在我的 build.gradle 中添加一个 mapstruct 依赖项。我还尝试为我的 lombok 依赖项定义一个版本(1.18.20,与我的 Eclipse 安装相同),这是以前未设置的。

这两个动作独立地解决了这个问题,但又创造了另一个:

2021-04-29 12:36:51.104 ERROR 4971 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to introspect Class [springfox.documentation.schema.configuration.ModelsConfiguration] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@21213b92]
    at spring.core@5.2.8.RELEASE/org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481) ~[spring-core-5.2.8.RELEASE.jar:na]
    at spring.core@5.2.8.RELEASE/org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:358) ~[spring-core-5.2.8.RELEASE.jar:na]
    at spring.core@5.2.8.RELEASE/org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:414) ~[spring-core-5.2.8.RELEASE.jar:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.lambda$getTypeForFactoryMethod$2(AbstractAutowireCapableBeanFactory.java:742) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708) ~[na:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:741) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:680) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:648) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1614) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:523) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:495) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at spring.context@5.2.8.RELEASE/org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:100) ~[spring-context-5.2.8.RELEASE.jar:na]
    at spring.context@5.2.8.RELEASE/org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:707) ~[spring-context-5.2.8.RELEASE.jar:na]
    at spring.context@5.2.8.RELEASE/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:533) ~[spring-context-5.2.8.RELEASE.jar:na]
    at spring.boot@2.3.2.RELEASE/org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.2.RELEASE.jar:na]
    at spring.boot@2.3.2.RELEASE/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.2.RELEASE.jar:na]
    at spring.boot@2.3.2.RELEASE/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.2.RELEASE.jar:na]
    at spring.boot@2.3.2.RELEASE/org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.2.RELEASE.jar:na]
    at spring.boot@2.3.2.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.2.RELEASE.jar:na]
    at spring.boot@2.3.2.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.2.RELEASE.jar:na]
    at spring.boot@2.3.2.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.2.RELEASE.jar:na]
    at com.feudjey.picsession.PicSessionApplication.main(PicSessionApplication.java:11) ~[main/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
    at spring.boot.devtools@2.4.5/org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.4.5.jar:na]
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/classmate/TypeResolver
    at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
    at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3325) ~[na:na]
    at java.base/java.lang.Class.getDeclaredMethods(Class.java:2466) ~[na:na]
    at spring.core@5.2.8.RELEASE/org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463) ~[spring-core-5.2.8.RELEASE.jar:na]
    ... 26 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.fasterxml.classmate.TypeResolver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606) ~[na:na]
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[na:na]
    ... 30 common frames omitted

2021-04-29 12:36:51.120  WARN 4971 --- [  restartedMain] o.s.boot.SpringApplication               : Unable to close ApplicationContext

java.lang.IllegalStateException: Failed to introspect Class [springfox.documentation.schema.configuration.ModelsConfiguration] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@21213b92]
    at spring.core@5.2.8.RELEASE/org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481) ~[spring-core-5.2.8.RELEASE.jar:na]
    at spring.core@5.2.8.RELEASE/org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:358) ~[spring-core-5.2.8.RELEASE.jar:na]
    at spring.core@5.2.8.RELEASE/org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:414) ~[spring-core-5.2.8.RELEASE.jar:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.lambda$getTypeForFactoryMethod$2(AbstractAutowireCapableBeanFactory.java:742) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708) ~[na:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:741) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:680) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:648) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1614) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:523) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:495) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:620) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at spring.beans@5.2.8.RELEASE/org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:612) ~[spring-beans-5.2.8.RELEASE.jar:na]
    at spring.context@5.2.8.RELEASE/org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1243) ~[spring-context-5.2.8.RELEASE.jar:na]
    at spring.boot@2.3.2.RELEASE/org.springframework.boot.SpringApplication.getExitCodeFromMappedException(SpringApplication.java:880) ~[spring-boot-2.3.2.RELEASE.jar:na]
    at spring.boot@2.3.2.RELEASE/org.springframework.boot.SpringApplication.getExitCodeFromException(SpringApplication.java:868) ~[spring-boot-2.3.2.RELEASE.jar:na]
    at spring.boot@2.3.2.RELEASE/org.springframework.boot.SpringApplication.handleExitCode(SpringApplication.java:855) ~[spring-boot-2.3.2.RELEASE.jar:na]
    at spring.boot@2.3.2.RELEASE/org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:806) ~[spring-boot-2.3.2.RELEASE.jar:na]
    at spring.boot@2.3.2.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:325) ~[spring-boot-2.3.2.RELEASE.jar:na]
    at spring.boot@2.3.2.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.2.RELEASE.jar:na]
    at spring.boot@2.3.2.RELEASE/org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.2.RELEASE.jar:na]
    at com.feudjey.picsession.PicSessionApplication.main(PicSessionApplication.java:11) ~[main/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
    at spring.boot.devtools@2.4.5/org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.4.5.jar:na]
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/classmate/TypeResolver
    at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
    at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3325) ~[na:na]
    at java.base/java.lang.Class.getDeclaredMethods(Class.java:2466) ~[na:na]
    at spring.core@5.2.8.RELEASE/org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463) ~[spring-core-5.2.8.RELEASE.jar:na]
    ... 26 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.fasterxml.classmate.TypeResolver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606) ~[na:na]
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[na:na]
    ... 30 common frames omitted

问题是当我运行 gradle 依赖项时,我的项目中有“com.fasterxml:classmate:1.5.1”。我发现了很多关于这种依赖的线程,但通常用户没有将它包含在他们的项目中,这不是我的情况。

另一件奇怪的事情是,当我隐藏我的更改以回到我上次良好的构建状态时,我遇到了关于 lombok 的相同的第一个错误。

我 4 年前开始使用 java 编码,我总是在这个社区论坛中找到解决我的问题的方法,但今天我完全迷失了,这就是我写第一个线程的原因。

这是我的 build.gradle :

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:'2.4.5'"
    }
}

plugins {
    id 'org.springframework.boot' version '2.3.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'eclipse'
    id 'java'
    id 'war'
}

bootWar {
    archiveName 'rest-api.war'
}

group = 'com.feudjey'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'

    // Security
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat', version: '2.4.3'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.3.3.RELEASE'
    implementation group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'

    // Swagger
    implementation group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.3'


    // File management
    implementation group: 'commons-io', name: 'commons-io', version: '2.8.0'
    implementation group: 'org.imgscalr', name: 'imgscalr-lib', version: '4.2'
    implementation group: 'com.drewnoakes', name: 'metadata-extractor', version: '2.14.0'

    // Validation
    implementation group: 'org.hibernate', name: 'hibernate-validator-annotation-processor', version: '7.0.1.Final'
    implementation group: 'org.hibernate.validator', name: 'hibernate-validator', version: '7.0.1.Final'
    implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
    implementation group: 'javax.el', name: 'javax.el-api', version: '3.0.0'
    implementation group: 'org.glassfish', name: 'javax.el', version: '3.0.0'

    // Misc
    implementation group: 'com.google.api-client', name: 'google-api-client', version: '1.30.10'
    
    // Mapper
    implementation group: 'org.modelmapper', name: 'modelmapper', version: '2.4.2'
    
    // Faker
    implementation group: 'com.github.javafaker', name: 'javafaker', version: '1.0.2'

    // eMail
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '2.4.2'

    // SMS
    implementation group: 'com.smsfactor', name: 'smsfactor', version: '1.0' 

    // Paypal
    implementation group: 'com.paypal.sdk', name: 'rest-api-sdk', version: '1.14.0'

    // Persistance
    implementation group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'



    // Database
    runtimeOnly group: 'org.postgresql', name: 'postgresql', version: '42.2.20'

    // Dev Tools
    developmentOnly group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '2.4.5'
   
    // Lombok
    compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.20'

    // Tests
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

javac 11.0.10

openjdk 版本“11.0.10” 2021-01-19

OpenJDK 运行时环境(内部版本 11.0.10+9)

OpenJDK 64 位服务器 VM(内部版本 11.0.10+9,混合模式)

编辑 :

当我删除这两个依赖项(以及使用它们的代码)

implementation group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
implementation group: 'com.fasterxml.jackson.core:jackson-databind', version: '2.9.6'

我收到以下错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field repository in com.feudjey.picsession.services.ServiceBase required a bean named 'entityManagerFactory' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

这有道理吗 ?

这是我的配置类:

package com.feudjey.picsession.configuration;

import java.time.OffsetDateTime;
import java.util.Optional;

import org.modelmapper.ModelMapper;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.data.auditing.DateTimeProvider;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import com.feudjey.picsession.contracts.GlobalContract;

@Configuration
@EnableJpaAuditing(dateTimeProviderRef = "auditingDateTimeProvider")
@EnableJpaRepositories(basePackages = {GlobalContract.BASE_PACKAGE + ".repositories"})
@EntityScan(basePackages = {GlobalContract.BASE_PACKAGE + ".entities"})
@ComponentScan(basePackages = {
        GlobalContract.BASE_PACKAGE + ".events",
        GlobalContract.BASE_PACKAGE + ".services",
        GlobalContract.BASE_PACKAGE + ".mappers",
        GlobalContract.BASE_PACKAGE + ".fixtures"})
public class ApplicationConfig {

    @Bean
    public ModelMapper modelMapper() {
        return new ModelMapper();
    }

//    @Bean
//    public Docket api() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .select()
//                .apis(RequestHandlerSelectors.basePackage(GlobalContract.BASE_PACKAGE + ".controllers"))
//                .paths(PathSelectors.any())
//                .build();
//    }

    @Bean
    public DateTimeProvider auditingDateTimeProvider() {
        return () -> Optional.of(OffsetDateTime.now());
    }

    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final String origin = "*";
        final String header = "*";
        final String method = "*";

        final CorsConfiguration config = new CorsConfiguration();

        config.setAllowCredentials(true);
        config.addAllowedOrigin(origin);
        config.addAllowedHeader(header);
        config.addAllowedMethod(method);

        source.registerCorsConfiguration("/**", config);

        return new CorsFilter(source);
    }

    @Bean
    public ApplicationEventMulticaster applicationEventMulticaster() {
        final SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();

        eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor());

        return eventMulticaster;
    }
}

编辑 2:

好的,我刚刚注意到该项目在 gradle bootrun 中完美运行,但在 eclipse 中没有,所以我没有调试模式。在每次 Eclipse 构建之前我都会做一个 gradle cleaneclipse eclipse,那会是什么?

如果您想了解有关我的项目的更多详细信息,请询问我。希望可以有人帮帮我。

谢谢。

标签: javaspring-bootgradlelombokfasterxml

解决方案


推荐阅读