英文:
r2dbc ReactiveCrudRepository keeps crashing
问题
以下是您提供的内容的翻译部分:
我的 pom.xml 文件有以下的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
<version>2.3.3.RELEASE</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-mssql</artifactId>
<scope>runtime</scope>
</dependency>
我的属性配置如下:
spring.r2dbc.url=r2dbc:mssql://localhost:1433/Admin
spring.r2dbc.username=xxx
spring.r2dbc.password=xxx
Repository 部分如下:
@Repository
public interface UserRepository extends ReactiveCrudRepository<MyUser, String> {
@Query("SELECT u.Username AS username, u.Password as password FROM Users u INNER JOIN Roles r ON u.RoleId=r.Roleid WHERE u.Username=@username")
public Mono<UserDetails> findByUsername(String username);
}
MyUser 部分如下:
@SuppressWarnings("serial")
public class MyUser implements UserDetails {
@Id
private String username;
private String password;
public MyUser(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return username;
}
@Override
public boolean isAccountNonExpired() {
return false;
}
@Override
public boolean isAccountNonLocked() {
return false;
}
@Override
public boolean isCredentialsNonExpired() {
return false;
}
@Override
public boolean isEnabled() {
return true;
}
}
我启用了 SQL 分析器并捕获了查询,查询是100%正确的并且返回了正确的数据,但似乎在反序列化时出现了崩溃。我设置了断点,但从未调用过 MyUser 构造函数。如果我手动使用 Mono.just() 创建一个,应用程序按预期工作。
我收到了以下异常:
java.lang.NullPointerException: null
at org.springframework.data.r2dbc.core.DefaultDatabaseClient$DefaultTypedExecuteSpec.lambda$new$0(DefaultDatabaseClient.java:590) ~[spring-data-r2dbc-1.1.3.RELEASE.jar:1.1.3.RELEASE]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ org.springframework.security.web.server.authentication.AuthenticationWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.authentication.AuthenticationWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.context.ReactorContextWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.header.HttpHeaderWriterWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.config.web.server.ServerHttpSecurity$ServerWebExchangeReactorContextWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.WebFilterChainProxy [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ HTTP POST "/login?error" [ExceptionHandlingWebHandler]
Stack trace:
at org.springframework.data.r2dbc.core.DefaultDatabaseClient$DefaultTypedExecuteSpec.lambda$new$0(DefaultDatabaseClient.java:590) ~[spring-data-r2dbc-1.1.3.RELEASE.jar:1.1.3.RELEASE]
at java.base/java.util.function.BiFunction.lambda$andThen$0(BiFunction.java:70) ~[na:na]
at io.r2dbc.mssql.MssqlResult.lambda$map$2(MssqlResult.java:169) ~[r2dbc-mssql-0.8.4.RELEASE.jar:0.8.4.RELEASE]
对于问题的原因有什么想法吗?异常出现在以下代码块:
if (typeToRead.isInterface()) {
this.mappingFunction = ColumnMapRowMapper.INSTANCE
.andThen(map -> projectionFactory.createProjection(typeToRead, map));
} else {
this.mappingFunction = dataAccessStrategy.getRowMapper(typeToRead);
}
请注意,您提供的是一段代码,问题的原因可能涉及代码的其他部分。最好的方法是根据堆栈跟踪和代码上下文来排查这个问题。
英文:
My pom.xml has the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
<version>2.3.3.RELEASE</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-mssql</artifactId>
<scope>runtime</scope>
</dependency>
My properties looks like:
spring.r2dbc.url=r2dbc:mssql://localhost:1433/Admin
spring.r2dbc.username=xxx
spring.r2dbc.password=xxx
Repo looks like:
@Repository
public interface UserRepository extends ReactiveCrudRepository<MyUser, String> {
@Query("SELECT u.Username AS username, u.Password as password FROM Users u INNER JOIN Roles r ON u.RoleId=r.Roleid WHERE u.Username=@username")
public Mono<UserDetails> findByUsername(String username);
}
MyUser looks like:
@SuppressWarnings("serial")
public class MyUser implements UserDetails {
@Id
private String username;
private String password;
public MyUser(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return username;
}
@Override
public boolean isAccountNonExpired() {
return false;
}
@Override
public boolean isAccountNonLocked() {
return false;
}
@Override
public boolean isCredentialsNonExpired() {
return false;
}
@Override
public boolean isEnabled() {
return true;
}
I turned on the SQL profiler and caught the query coming in, and it is 100% correct and returns the correct data, but it seems to be crashing on the deserialization. I set a breakpoint and it never calls the MyUser contructor. If I manually create one with Mono.just(), the app works as expected.
I am getting the following exception:
java.lang.NullPointerException: null
at org.springframework.data.r2dbc.core.DefaultDatabaseClient$DefaultTypedExecuteSpec.lambda$new$0(DefaultDatabaseClient.java:590) ~[spring-data-r2dbc-1.1.3.RELEASE.jar:1.1.3.RELEASE]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ org.springframework.security.web.server.authentication.AuthenticationWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.authentication.AuthenticationWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.context.ReactorContextWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.header.HttpHeaderWriterWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.config.web.server.ServerHttpSecurity$ServerWebExchangeReactorContextWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.security.web.server.WebFilterChainProxy [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ HTTP POST "/login?error" [ExceptionHandlingWebHandler]
Stack trace:
at org.springframework.data.r2dbc.core.DefaultDatabaseClient$DefaultTypedExecuteSpec.lambda$new$0(DefaultDatabaseClient.java:590) ~[spring-data-r2dbc-1.1.3.RELEASE.jar:1.1.3.RELEASE]
at java.base/java.util.function.BiFunction.lambda$andThen$0(BiFunction.java:70) ~[na:na]
at io.r2dbc.mssql.MssqlResult.lambda$map$2(MssqlResult.java:169) ~[r2dbc-mssql-0.8.4.RELEASE.jar:0.8.4.RELEASE]
Any ideas on the what the issue is? The exception is on this block of code:
if (typeToRead.isInterface()) {
this.mappingFunction = ColumnMapRowMapper.INSTANCE
.andThen(map -> projectionFactory.createProjection(typeToRead, map));
} else {
this.mappingFunction = dataAccessStrategy.getRowMapper(typeToRead);
}
答案1
得分: 1
-
如果您正在使用 Repository,在您的代码中,
MyUser
未使用@Table
进行注释,例如这里。 -
如果您不使用 Spring Data R2dbc 中提供的对象关系映射(OR Mapping),您可以尝试使用
DatabaseClient
手动处理查询,如果您坚持编写自定义 SQL 脚本,这样做更灵活。例如这里。
英文:
答案2
得分: 0
尝试在你的MyUser类中添加一个默认构造函数(没有参数的构造函数)。
英文:
Try adding a default constructor (a constructor with no parameters) to your MyUser class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论