【问题标题】:Why unable to recogize getter method of One Class to another class in java?java - 为什么无法识别一个类的getter方法到另一个类?
【发布时间】:2020-08-30 05:13:21
【问题描述】:

我没有什么要说或解释的,我面临的问题是在我的程序的某些部分,它无法识别 getter 方法,但是我创建了一个 getter 方法并且它是公开的也是。

用户类:

public class User {

     @Id
        @Column(name="id")
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;
        private String firstname;
        private String lastname;
        private String email;
        private String password;
        @ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
        @JoinTable(
                name="users_roles",
                joinColumns = @JoinColumn(
                        name= "user_id",referencedColumnName = "id"),
                inverseJoinColumns = @JoinColumn(
                        name= "role_id",referencedColumnName = "rollid"))
        private Set<Role> roles;
    
        /*Creating cart*/
        @OneToMany(fetch = FetchType.EAGER)
        @JoinTable(
                name="user_purchase",
                joinColumns = @JoinColumn(
                        name="user_id",referencedColumnName = "id"),
                inverseJoinColumns = @JoinColumn(
                        name="product_id",referencedColumnName = "productId"))
        private Set<Products> purchased;
    
        /*All-arg constructor and No-arg Constructor*/
        public User() {}
    
        public User(String firstname, String lastname,
                    String email, String password, Set<Role> roles, Set<Products> purchased) {
            this.firstname = firstname;
            this.lastname = lastname;
            this.email = email;
            this.password = password;
            this.roles = roles;
            this.purchased = purchased;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getFirstname() {
            return firstname;
        }
    
        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }
    
        public String getLastname() {
            return lastname;
        }
    
        public void setLastname(String lastname) {
            this.lastname = lastname;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public Set<Role> getRoles() {
            return roles;
        }
    
        public void setRoles(Set<Role> roles) {
            this.roles = roles;
        }
    
        public Set<Products> getPurchased() {
            return purchased;
        }
    
        public void setPurchased(Set<Products> purchased) {
            this.purchased = purchased;
        }
    }

下面,我发布的是 CustomUserDetails 类:*

package com.ecommerce.demo.services;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.stream.Collectors;

public class CustomUserDetails implements UserDetails {

    private User user;
    /*Getter and Setters for user variable*/

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    /*Overrided Methods Of User Details Class*/
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return user.getRoles().stream().
                map(role-> new SimpleGrantedAuthority("ROLE_"+role)).collection(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}

在上面的代码中,返回语句 IntelliJ 显示 getRoles() 未定义。

【问题讨论】:

  • 确保您使用的是相同的User 类。
  • @chrylis-cautiouslyoptimistic-先生,我的程序中只有一个用户类
  • @chrylis-cautiouslyoptimistic- 是的,你写的是……但现在我收到了could not resolve method collection in stream
  • @deep 你能分享你对 CustomUserDetails 类的导入吗
  • @PrathibhaChiranthana 我已经更新了 CustomUserDetails 类

标签: java spring java-stream


【解决方案1】:

问题在于您的用户导入。您需要在 CustomUserDetails 类中导入正确的用户类。消除 导入 org.springframework.security.core.userdetails.User;

并将其替换为您的用户类。

        import org.springframework.security.core.GrantedAuthority;
        import org.springframework.security.core.authority.SimpleGrantedAuthority;
        //Remove Below import 
        import org.springframework.security.core.userdetails.User;
        
       //Add your User class Here
       import YOURPACKAGE.User;

        import org.springframework.security.core.userdetails.UserDetails;
        
        import java.util.Collection;
        import java.util.stream.Collectors;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多