在我的spring boot应用程序中,我面临以下问题。
在build.gradle文件中添加了以下依赖项
compile('org.springframework.boot:spring-boot-starter-data-redis-reactive')
已添加到Application.java文件中
@Bean
public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() {
return new LettuceConnectionFactory("localhost", 6379);
}
@Bean
public ReactiveRedisTemplate<String, String> reactiveRedisTemplateString() {
return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory(),
RedisSerializationContext.string());
}
在服务类ApplicationService.java
中@Autowired
ReactiveRedisTemplate<String, String> reactiveRedisTemplateString;
,我正在使用此模板向Redis添加键和值。 启动服务器时,我遇到了下面的异常。但是,如果我在Application.java类的下面添加行,则不会出现该异常,但是Controller类的请求映射将不起作用。
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,WebMvcAutoConfiguration.class })
遇到错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is java.lang.NoSuchFieldError: defaultReader
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:625) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:605) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1288) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at com.falabella.turnero.TurneroApplication.main(TurneroApplication.java:68) [main/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.0.RELEASE.jar:2.1.0.RELEASE]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is java.lang.NoSuchFieldError: defaultReader
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:620) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
... 24 common frames omitted
Caused by: java.lang.NoSuchFieldError: defaultReader
此错误仅出现在我的项目中,我将创建一个简单的项目以供Redis插入,然后它将起作用。请帮我解决问题。
下面是build.gradle文件
buildscript {
ext {
springBootVersion = '2.1.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'java'
}
plugins {
id 'net.ltgt.apt' version '0.10'
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'pmd'
apply plugin: 'checkstyle'
group 'my-services'
version '0.1.0'
sourceCompatibility = 1.8
compileJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
repositories {
mavenCentral()
}
tasks.withType(Checkstyle){
reports{
xml.enabled=true
html.enabled=true
}
}
tasks.withType(Pmd){
reports{
xml.enabled=true
html.enabled=true
}
}
pmd {
pmdTest.enabled=true
ruleSets = [
'java-basic',
'java-braces',
'java-empty',
'java-strings',
'java-unnecessary'
]
}
dependencies {
compile ('org.springframework.boot:spring-boot-starter-web:2.0.6.RELEASE')
compile ('org.springframework.boot:spring-boot-starter-webflux:2.0.6.RELEASE')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile "org.projectlombok:lombok:1.18.4"
annotationProcessor 'org.projectlombok:lombok:1.18.4'
compile ('com.google.firebase:firebase-admin:6.5.0')
compile('io.micrometer:micrometer-registry-prometheus')
compile ('io.springfox:springfox-swagger2:2.9.2')
compile ('io.springfox:springfox-swagger-ui:2.9.2')
compile('com.microsoft.azure:azure-eventgrid:1.2.0')
compile group: 'com.microsoft.azure', name: 'azure-servicebus-spring-boot-starter', version: '2.0.7'
compile group: 'org.springframework.retry', name: 'spring-retry', version: '1.2.2.RELEASE'
compile group: 'org.springframework', name: 'spring-aop', version: '5.1.2.RELEASE'
compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.2'
compile('org.springframework.boot:spring-boot-starter-data-redis')
compile('org.apache.commons:commons-lang3:3.7')
compile('commons-io:commons-io:2.6')
runtime ('org.springframework.boot:spring-boot-devtools:2.0.6.RELEASE')
testCompile ('org.springframework.boot:spring-boot-starter-test:2.0.6.RELEASE')
testCompile ('junit:junit:4.12')
testCompile ('com.squareup.okhttp3:mockwebserver:3.9.0')
}
下面是更新的build.gradle文件。
buildscript {
ext {
springBootVersion = '2.1.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'java'
}
plugins {
id 'net.ltgt.apt' version '0.10'
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'pmd'
apply plugin: 'checkstyle'
group 'my-services'
version '0.1.0'
sourceCompatibility = 1.8
compileJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
repositories {
mavenCentral()
}
tasks.withType(Checkstyle){
reports{
xml.enabled=true
html.enabled=true
}
}
tasks.withType(Pmd){
reports{
xml.enabled=true
html.enabled=true
}
}
pmd {
pmdTest.enabled=true
ruleSets = [
'java-basic',
'java-braces',
'java-empty',
'java-strings',
'java-unnecessary'
]
}
dependencies {
compile ('org.springframework.boot:spring-boot-starter-web')
compile ('org.springframework.boot:spring-boot-starter-webflux')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile "org.projectlombok:lombok:1.18.4"
annotationProcessor 'org.projectlombok:lombok:1.18.4'
compile ('com.google.firebase:firebase-admin:6.5.0')
compile('io.micrometer:micrometer-registry-prometheus')
compile ('io.springfox:springfox-swagger2:2.9.2')
compile ('io.springfox:springfox-swagger-ui:2.9.2')
compile('com.microsoft.azure:azure-eventgrid:1.2.0')
compile group: 'com.microsoft.azure', name: 'azure-servicebus-spring-boot-starter', version: '2.0.7'
//compile group: 'org.springframework', name: 'spring-aop', version: '5.1.2.RELEASE'
//compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.2'
compile('org.springframework.boot:spring-boot-starter-aop')
compile('org.springframework.retry:spring-retry')
compile('org.springframework.boot:spring-boot-starter-data-redis')
compile('org.apache.commons:commons-lang3:3.7')
compile('commons-io:commons-io:2.6')
runtime ('org.springframework.boot:spring-boot-devtools')
testCompile ('org.springframework.boot:spring-boot-starter-test')
testCompile ('junit:junit:4.12')
testCompile ('com.squareup.okhttp3:mockwebserver:3.9.0')
}