java.util.util.optional< t> gt;不能应用于给定类型
我的代码在flatmap中给出了错误。
public Optional<User> getUserWithAuthorities() {
return SecurityUtils.getCurrentUserLogin().flatMap(UserDao::findOneWithAuthoritiesByLogin);
}
以下是错误:
controller/AuthenticationController.java:[77,51] method flatMap in class java.util.Optional<T> cannot be applied to given types;
[ERROR] required: java.util.function.Function<? super java.lang.String,java.util.Optional<U>>
[ERROR] found: UserDao::f[...]Login
[ERROR] reason: cannot infer type-variable(s) U
[ERROR] (argument mismatch; invalid method reference
[ERROR] cannot find symbol
[ERROR] symbol: method findOneWithAuthoritiesByLogin(java.lang.String)
[ERROR] location: interface net.javaguides.springboot.dao.UserDao)
这是UserDao代码:
@Repository
public interface UserDao extends JpaRepository<User, Long> {
User findByUsername(String username);
Optional<User> findOneWithAuthoritiesByLogin(String login);
}
这是SecurityUtils代码:
public final class SecurityUtils {
private SecurityUtils() {}
/**
* Get the login of the current user.
*
* @return the login of the current user.
*/
public static Optional<String> getCurrentUserLogin() {
SecurityContext securityContext = SecurityContextHolder.getContext();
return Optional.ofNullable(extractPrincipal(securityContext.getAuthentication()));
}
private static String extractPrincipal(Authentication authentication) {
if (authentication == null) {
return null;
} else if (authentication.getPrincipal() instanceof UserDetails) {
UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
return springSecurityUser.getUsername();
} else if (authentication.getPrincipal() instanceof String) {
return (String) authentication.getPrincipal();
}
return null;
}
问题是什么?
My code is giving an error in flatmap.
public Optional<User> getUserWithAuthorities() {
return SecurityUtils.getCurrentUserLogin().flatMap(UserDao::findOneWithAuthoritiesByLogin);
}
Below is the error:
controller/AuthenticationController.java:[77,51] method flatMap in class java.util.Optional<T> cannot be applied to given types;
[ERROR] required: java.util.function.Function<? super java.lang.String,java.util.Optional<U>>
[ERROR] found: UserDao::f[...]Login
[ERROR] reason: cannot infer type-variable(s) U
[ERROR] (argument mismatch; invalid method reference
[ERROR] cannot find symbol
[ERROR] symbol: method findOneWithAuthoritiesByLogin(java.lang.String)
[ERROR] location: interface net.javaguides.springboot.dao.UserDao)
This is UserDao code:
@Repository
public interface UserDao extends JpaRepository<User, Long> {
User findByUsername(String username);
Optional<User> findOneWithAuthoritiesByLogin(String login);
}
This is SecurityUtils code:
public final class SecurityUtils {
private SecurityUtils() {}
/**
* Get the login of the current user.
*
* @return the login of the current user.
*/
public static Optional<String> getCurrentUserLogin() {
SecurityContext securityContext = SecurityContextHolder.getContext();
return Optional.ofNullable(extractPrincipal(securityContext.getAuthentication()));
}
private static String extractPrincipal(Authentication authentication) {
if (authentication == null) {
return null;
} else if (authentication.getPrincipal() instanceof UserDetails) {
UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
return springSecurityUser.getUsername();
} else if (authentication.getPrincipal() instanceof String) {
return (String) authentication.getPrincipal();
}
return null;
}
What is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误是由对接口类
userdao :: findOneWithAuthoritiesBylogin
引起的。 Java不能单独使用接口调用该方法。要解决它,而是请参考类型
userdao
的实例化对象。这是接口的示例实现:
然后创建一个实例并引用它而不是接口类:
在这种情况下,
getUserWithAuthorities
方法现在返回可选
包含>用户
对象。The error is caused by the reference to the interface class
UserDao::findOneWithAuthoritiesByLogin
. Java cannot call the method using the interface alone.To solve it, instead refer to an instantiated object of type
UserDao
.Here's an example implementation of the interface:
Then create an instance and reference it instead of the interface class:
In this case, the
getUserWithAuthorities
method now returns anOptional
containing aUser
object.