为什么在Java中一个类无法识别另一个类的getter方法?

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

Why unable to recogize getter method of One Class to another class in java?

问题

我没有太多要说或解释的,我面临的问题在我的程序的某个部分,它无法识别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;

    /*创建购物车*/
    @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;

    /*全参构造函数和无参构造函数*/
    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;
    /*user变量的Getter和Setter方法*/

    public User getUser() {
        return user;
    }

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

    /*User Details类的重写方法*/
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return user.getRoles().stream().
                map(role-> new SimpleGrantedAuthority("ROLE_"+role)).collect(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() 未定义。

英文:

I don't have nothing much to say or explain, the problem that I'm facing is in certain part of my program, it is unable to recognize the getter method, but I have created a getter method and it is public as well.

User Class:

public class User {
@Id
@Column(name=&quot;id&quot;)
@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=&quot;users_roles&quot;,
joinColumns = @JoinColumn(
name= &quot;user_id&quot;,referencedColumnName = &quot;id&quot;),
inverseJoinColumns = @JoinColumn(
name= &quot;role_id&quot;,referencedColumnName = &quot;rollid&quot;))
private Set&lt;Role&gt; roles;
/*Creating cart*/
@OneToMany(fetch = FetchType.EAGER)
@JoinTable(
name=&quot;user_purchase&quot;,
joinColumns = @JoinColumn(
name=&quot;user_id&quot;,referencedColumnName = &quot;id&quot;),
inverseJoinColumns = @JoinColumn(
name=&quot;product_id&quot;,referencedColumnName = &quot;productId&quot;))
private Set&lt;Products&gt; purchased;
/*All-arg constructor and No-arg Constructor*/
public User() {}
public User(String firstname, String lastname,
String email, String password, Set&lt;Role&gt; roles, Set&lt;Products&gt; 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&lt;Role&gt; getRoles() {
return roles;
}
public void setRoles(Set&lt;Role&gt; roles) {
this.roles = roles;
}
public Set&lt;Products&gt; getPurchased() {
return purchased;
}
public void setPurchased(Set&lt;Products&gt; purchased) {
this.purchased = purchased;
}
}

Below, I'm posting the CustomUserDetails class:*

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&lt;? extends GrantedAuthority&gt; getAuthorities() {
return user.getRoles().stream().
map(role-&gt; new SimpleGrantedAuthority(&quot;ROLE_&quot;+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;
}
}

In above code in return statement IntelliJ showing me getRoles() is not defined.

答案1

得分: 2

问题出在您的用户导入上。您需要在CustomUserDetails类中导入正确的用户类。请删除以下导入:

import org.springframework.security.core.userdetails.User;

然后将其替换为您的User类:

import YOURPACKAGE.User;

同时,请确保您已经添加了以下导入:

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.util.Collection;
import java.util.stream.Collectors;
英文:

The problem is with your User import. You need to import the correct user class in the CustomUserDetails class. Remove
import org.springframework.security.core.userdetails.User;

And replace it with your User class.

        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;

huangapple
  • 本文由 发表于 2020年8月30日 13:13:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63654163.html
匿名

发表评论

匿名网友

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

确定