英文:
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="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;
}
}
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<? 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;
}
}
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论