r2dbc ReactiveCrudRepository 持续崩溃

huangapple go评论79阅读模式
英文:

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:

&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-r2dbc&lt;/artifactId&gt;
&lt;version&gt;2.3.3.RELEASE&lt;/version&gt;&lt;!--$NO-MVN-MAN-VER$--&gt;
&lt;/dependency&gt;		
&lt;dependency&gt;
&lt;groupId&gt;io.r2dbc&lt;/groupId&gt;
&lt;artifactId&gt;r2dbc-mssql&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;

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&lt;MyUser, String&gt; {
@Query(&quot;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&quot;) 
public Mono&lt;UserDetails&gt; findByUsername(String username);
}

MyUser looks like:

@SuppressWarnings(&quot;serial&quot;)
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&lt;? extends GrantedAuthority&gt; 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 &quot;/login?error&quot; [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 -&gt; projectionFactory.createProjection(typeToRead, map));
} else {
this.mappingFunction = dataAccessStrategy.getRowMapper(typeToRead);
}

答案1

得分: 1

  1. 如果您正在使用 Repository,在您的代码中,MyUser 未使用 @Table 进行注释,例如这里

  2. 如果您不使用 Spring Data R2dbc 中提供的对象关系映射(OR Mapping),您可以尝试使用 DatabaseClient 手动处理查询,如果您坚持编写自定义 SQL 脚本,这样做更灵活。例如这里

英文:
  1. If you are using the Repository, in your codes, MyUser is not annotated with @Table, eg here

  2. You could try to use DatabaseClient to handle the query manually if you do not use the OR Mapping provided in Spring Data R2dbc, it is more flexible if you are stick on writing custom SQL scripts. eg. here.

答案2

得分: 0

尝试在你的MyUser类中添加一个默认构造函数(没有参数的构造函数)。

英文:

Try adding a default constructor (a constructor with no parameters) to your MyUser class.

huangapple
  • 本文由 发表于 2020年9月9日 11:01:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63804165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定