如何解决R2dbc 和 Jdbc Probelm Spring Boot
大家好,我使用 Spring Boot Webflux 编写了一个 rest api
标签控制器
@RestController
@RequestMapping("/api/label")
@RequiredArgsConstructor
public class LabelController {
private final LabelService labelService;
@GetMapping
public Flux<LabelEntity> getAllLabels(){
return labelService.getAll();
}
标签服务
@Service
@RequiredArgsConstructor
public class LabelService {
private final LabelRepository labelRepository;
public Flux<LabelEntity> getAll() {
return labelRepository.findAll();
}
LabelRespository
@Repository
public interface LabelRepository extends ReactiveCrudRepository<LabelEntity,Integer> {
}
应用程序.yml 由于我在应用yml部分使用了liquebase,所以添加了jdbc和r2dbc。
spring:
liquibase:
enabled: true
url: jdbc:postgresql://localhost:5432/liquebase
user: postgres
password: 12345
change-log: classpath:db/liquibase/db.changelog-master-main.yml
datasource:
driver-class-name: org.postgresql.Driver
r2dbc:
url: r2dbc:postgresql://localhost:5432/liquebase
username: postgres
password: 12345
pool:
initial-size: 100
max-size: 500
max-idle-time: 30m
validation-query: SELECT 1
server:
port: 8085
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.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.WebFluxTestCodes</groupId>
<artifactId>WebFluxTestCodes</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>WebFluxTestCodes</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.10.3</version>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
我的 RestApi 主类
@SpringBootApplication
@EnableR2dbcRepositories
@ComponentScan("com.WebFluxTestCodes.WebFluxTestCodes.business.*")
public class WebFluxTestCodesApplication {
public static void main(String[] args) {
SpringApplication.run(WebFluxTestCodesApplication.class,args);
}
}
在项目启动时记录我的项目没有问题
2021-03-15 20:38:12.375 INFO 8692 --- [ restartedMain] c.W.W.WebFluxTestCodesApplication :
Starting WebFluxTestCodesApplication using Java 15.0.1 on WIN-3QMQSJHKUBK with PID 8692
(C:\Users\user\Desktop\TestWebService\WebFluxTestCodes\target\classes started by user in
C:\Users\user\Desktop\TestWebService\WebFluxTestCodes)
2021-03-15 20:38:12.375 INFO 8692 --- [ restartedMain] c.W.W.WebFluxTestCodesApplication :
No active profile set,falling back to default profiles: default
2021-03-15 20:38:13.266 INFO 8692 --- [ restartedMain] o.s.b.devtools.restart.ChangeableUrls :
The Class-Path manifest attribute in C:\Users\user\.m2\repository\org\liquibase\liquibase-
core\3.10.3\liquibase-core-3.10.3.jar referenced one or more files that do not exist:
file:/C:/Users/user/.m2/repository/org/liquibase/liquibase-core/3.10.3/commons-cli-
1.4.jar,file:/C:/Users/user/.m2/repository/org/liquibase/liquibase-core/3.10.3/snakeyaml-
1.24.jar,file:/C:/Users/user/.m2/repository/org/liquibase/liquibase-core/3.10.3/slf4j-api-
1.7.28.jar,file:/C:/Users/user/.m2/repository/org/liquibase/liquibase-core/3.10.3/logback-classic-
1.2.3.jar,file:/C:/Users/user/.m2/repository/org/liquibase/liquibase-core/3.10.3/logback-core-
1.2.3.jar
2021-03-15 20:38:13.266 INFO 8692 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor :
Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-03-15 20:38:13.266 INFO 8692 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor :
For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-03-15 20:38:16.782 INFO 8692 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate :
Multiple Spring Data modules found,entering strict repository configuration mode!
2021-03-15 20:38:16.782 INFO 8692 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate
: Bootstrapping Spring Data R2DBC repositories in DEFAULT mode.
2021-03-15 20:38:17.264 INFO 8692 --- [ restartedMain] .RepositoryConfigurationExtensionSupport
: Spring Data R2DBC - Could not safely identify store assignment for repository candidate
interface com.WebFluxTestCodes.WebFluxTestCodes.repository.LabelRepository. If you want this
repository to be a R2DBC repository,consider annotating your entities with one of these
annotations: org.springframework.data.relational.core.mapping.Table (preferred),or consider
extending one of the following types with your repository:
org.springframework.data.r2dbc.repository.R2dbcRepository.
2021-03-15 20:38:17.276 INFO 8692 --- [ restartedMain]
.RepositoryConfigurationExtensionSupport : Spring Data R2DBC - Could not safely identify store
assignment for repository candidate interface
com.WebFluxTestCodes.WebFluxTestCodes.repository.ProjectRepository. If you want this repository
to be a R2DBC repository,consider annotating your entities with one of these annotations:
org.springframework.data.relational.core.mapping.Table (preferred),or consider extending one of
the following types with your repository:
org.springframework.data.r2dbc.repository.R2dbcRepository.
2021-03-15 20:38:17.280 INFO 8692 --- [ restartedMain] .RepositoryConfigurationExtensionSupport
: Spring Data R2DBC - Could not safely identify store assignment for repository candidate
interface com.WebFluxTestCodes.WebFluxTestCodes.repository.TasksRepository. If you want this
repository to be a R2DBC repository,consider annotating your entities with one of these
annotations: org.springframework.data.relational.core.mapping.Table (preferred),or consider
extending one of the following types with your repository:
org.springframework.data.r2dbc.repository.R2dbcRepository.
2021-03-15 20:38:17.284 INFO 8692 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate
: Finished Spring Data repository scanning in 452 ms. Found 0 R2DBC repository interfaces.
2021-03-15 20:38:18.522 INFO 8692 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate
: Multiple Spring Data modules found,entering strict repository configuration mode!
2021-03-15 20:38:18.624 INFO 8692 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate
: Bootstrapping Spring Data R2DBC repositories in DEFAULT mode.
2021-03-15 20:38:18.714 INFO 8692 --- [ restartedMain] .RepositoryConfigurationExtensionSupport
: Spring Data R2DBC - Could not safely identify store assignment for repository candidate
interface com.WebFluxTestCodes.WebFluxTestCodes.repository.LabelRepository. If you want this
repository to be a R2DBC repository,or consider
extending one of the following types with your repository:
org.springframework.data.r2dbc.repository.R2dbcRepository.
2021-03-15 20:38:18.718 INFO 8692 --- [ restartedMain]
.RepositoryConfigurationExtensionSupport : Spring Data R2DBC - Could not safely identify store
assignment for repository candidate interface
com.WebFluxTestCodes.WebFluxTestCodes.repository.ProjectRepository. If you want this repository
to be a R2DBC repository,consider annotating your entities with one of these annotations:
org.springframework.data.relational.core.mapping.Table (preferred),or consider extending one
of the following types with your repository:
org.springframework.data.r2dbc.repository.R2dbcRepository.
2021-03-15 20:38:18.723 INFO 8692 --- [ restartedMain] .RepositoryConfigurationExtensionSupport
: Spring Data R2DBC - Could not safely identify store assignment for repository candidate
interface com.WebFluxTestCodes.WebFluxTestCodes.repository.TasksRepository. If you want this
repository to be a R2DBC repository,or consider
extending one of the following types with your repository:
org.springframework.data.r2dbc.repository.R2dbcRepository.
2021-03-15 20:38:18.724 INFO 8692 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate
: Finished Spring Data repository scanning in 97 ms. Found 0 R2DBC repository interfaces.
2021-03-15 20:38:26.847 INFO 8692 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource
: HikariPool-1 - Starting...
2021-03-15 20:38:28.550 INFO 8692 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource
: HikariPool-1 - Start completed.
2021-03-15 20:38:42.546 INFO 8692 --- [ restartedMain] l.lockservice.StandardLockService
: Successfully acquired change log lock
2021-03-15 20:38:44.908 INFO 8692 --- [ restartedMain] l.c.StandardChangeLogHistoryService
: Reading from public.databasechangelog
2021-03-15 20:38:45.635 INFO 8692 --- [ restartedMain] l.lockservice.StandardLockService :
Successfully released change log lock
2021-03-15 20:38:45.651 INFO 8692 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource :
HikariPool-1 - Shutdown initiated...
2021-03-15 20:38:45.698 INFO 8692 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource
: HikariPool-1 - Shutdown completed.
2021-03-15 20:38:46.773 INFO 8692 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer
: LiveReload server is running on port 35729
2021-03-15 20:38:47.884 INFO 8692 --- [ restartedMain] o.s.b.web.embedded.netty.NettyWebServer :
Netty started on port 8085
2021-03-15 20:38:47.915 INFO 8692 --- [ restartedMain] c.W.W.WebFluxTestCodesApplication
: Started WebFluxTestCodesApplication in 39.523 seconds (JVM running for 45.043)
但是,当我使用浏览器发送请求时,没有请求到达控制器,浏览器中出现以下错误。
This application has no configured error view,so you are seeing this as a fallback.
Mon Mar 15 20:38:51 AZT 2021
[005bd2a6-1] There was an unexpected error (type=Not Found,status=404).
org.springframework.web.server.ResponseStatusException: 404 NOT_FOUND
at
org.springframework.web.reactive.resource.ResourceWebHandler
.lambda$handle$1(ResourceWebHandler.java:378)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ HTTP GET "/all" [ExceptionHandlingWebHandler]
Stack trace:
at
org.springframework.web.reactive.resource
.ResourceWebHandler.lambda$handle$1(ResourceWebHandler.java:378)
at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:44)
at reactor.core.publisher.Mono.subscribe(Mono.java:4046)
at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber
.onComplete(FluxSwitchIfEmpty.java:81)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.onComplete(MonoFlatMap.java:181)
at reactor.core.publisher.MonoNext$NextSubscriber.onComplete(MonoNext.java:102)
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.drain(FluxConcatMap.java:366)
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.onSubscribe(FluxConcatMap.java:218)
at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:164)
at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:86)
at reactor.core.publisher.Mono.subscribe(Mono.java:4046)
at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.drain(MonoIgnoreThen.java:173)
at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:56)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:157)
at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber
.onNext(FluxSwitchIfEmpty.java:73)
at reactor.core.publisher.MonoNext$NextSubscriber.onNext(MonoNext.java:82)
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.innerNext(FluxConcatMap.java:281)
at reactor.core.publisher.FluxConcatMap$ConcatMapInner.onNext(FluxConcatMap.java:860)
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:127)
at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:2397)
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:169)
at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.set(Operators.java:2193)
at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.onSubscribe(Operators.java:2067)
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber
.onSubscribe(FluxMapFuseable.java:96)
at reactor.core.publisher.MonoJust.subscribe(MonoJust.java:54)
at reactor.core.publisher.Mono.subscribe(Mono.java:4046)
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.drain(FluxConcatMap.java:448)
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.onSubscribe(FluxConcatMap.java:218)
at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:164)
at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:86)
at reactor.core.publisher.InternalMonoOperator.subscribe(InternalMonoOperator.java:64)
at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52)
at reactor.core.publisher.Mono.subscribe(Mono.java:4046)
at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.drain(MonoIgnoreThen.java:173)
at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:56)
at reactor.core.publisher.InternalMonoOperator.subscribe(InternalMonoOperator.java:64)
at reactor.netty.http.server.HttpServer$HttpServerHandle.onStateChange(HttpServer.java:860)
at reactor.netty.ReactorNetty$CompositeConnectionObserver.onStateChange(ReactorNetty.java:638)
at reactor.netty.transport.ServerTransport$ChildObserver.onStateChange(ServerTransport.java:478)
at reactor.netty.http.server.HttpServerOperations.onInboundNext(HttpServerOperations.java:525)
at reactor.netty.channel.ChannelOperationsHandler.channelRead(ChannelOperationsHandler.java:94)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at reactor.netty.http.server.HttpTrafficHandler.channelRead(HttpTrafficHandler.java:209)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:324)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:296)
at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:832)
请求控制器项目不工作 有什么问题?这对我很重要请帮助我
我的项目github链接 --https://github.com/Z-Farrukzada/Webflux-Reactive-Programming-example
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。