java.util.util.optional< t> gt;不能应用于给定类型

发布于 2025-02-13 22:53:28 字数 2157 浏览 1 评论 0原文

我的代码在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芸娘子的小脾气 2025-02-20 22:53:28

错误是由对接口类userdao :: findOneWithAuthoritiesBylogin引起的。 Java不能单独使用接口调用该方法。

要解决它,而是请参考类型userdao的实例化对象。

这是接口的示例实现:

    class UserDaoImplementation implements UserDao {
        @Override
        public Optional<User> findOneWithAuthoritiesByLogin(String login) {
            User user = User.builder().name("Tom").build();
            return Optional.of(user);
        }
    }

然后创建一个实例并引用它而不是接口类:

    public static Optional<User> getUserWithAuthorities() {
        UserDao userDao = new UserDaoImplementation();
        return SecurityUtils.getCurrentUserLogin().flatMap(userDao::findOneWithAuthoritiesByLogin);
    }

在这种情况下,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:

    class UserDaoImplementation implements UserDao {
        @Override
        public Optional<User> findOneWithAuthoritiesByLogin(String login) {
            User user = User.builder().name("Tom").build();
            return Optional.of(user);
        }
    }

Then create an instance and reference it instead of the interface class:

    public static Optional<User> getUserWithAuthorities() {
        UserDao userDao = new UserDaoImplementation();
        return SecurityUtils.getCurrentUserLogin().flatMap(userDao::findOneWithAuthoritiesByLogin);
    }

In this case, the getUserWithAuthorities method now returns an Optional containing a User object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文