当前位置: 首页 > 知识库问答 >
问题:

spring boot应用程序中Getting Class not Found异常错误

汪驰
2023-03-14

我的项目结构如所附图像所示

authority.java

package com.easkart.domain.security;

import org.springframework.security.core.GrantedAuthority;

public class Authority implements GrantedAuthority {

/**
 * 
 */
private static final long serialVersionUID = 5888173406859788817L;
private final String authority;

public Authority(String authority) {
    this.authority = authority;
}

@Override
public String getAuthority() {
    return authority;
}

}

role.java

package com.easkart.domain.security;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class Role {

@Id
private int roleId;
private String name;
@OneToMany(mappedBy = "role", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<UserRole> userRoles = new HashSet<>();

public int getRoleId() {
    return roleId;
}

public void setRoleId(int roleId) {
    this.roleId = roleId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Set<UserRole> getUserRoles() {
    return userRoles;
}

public void setUserRoles(Set<UserRole> userRoles) {
    this.userRoles = userRoles;
}

}

userRole.java

package com.easkart.domain.security;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.easkart.domain.User;


@Entity
@Table(name = "user_role")
public class UserRole {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long userRoleId;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User user;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "role_id")
private Role role;

public UserRole(User user, Role role) {
    this.user = user;
    this.role = role;
}

public long getUserRoleId() {
    return userRoleId;
}

public void setUserRoleId(long userRoleId) {
    this.userRoleId = userRoleId;
}

public User getUser() {
    return user;
}

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

public Role getRole() {
    return role;
}

public void setRole(Role role) {
    this.role = role;
}

}

user.java

package com.easkart.domain;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.easkart.domain.security.Authority;
import com.easkart.domain.security.UserRole;
import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
public class User implements UserDetails {
/**
 * 
 */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, updatable = false)
private long userId;
private String fullName;

@Column(name = "email", nullable = false, updatable = false)
private String email;

@Column(name = "phone", nullable = false, updatable = false)
private String phone;
private boolean enabled = true;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JsonIgnore
private Set<UserRole> userRoles = new HashSet<>();

public long getUserId() {
    return userId;
}

public void setUserId(long userId) {
    this.userId = userId;
}

public String getFullName() {
    return fullName;
}

public void setFullName(String fullName) {
    this.fullName = fullName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public void setEnabled(boolean enabled) {
    this.enabled = enabled;
}

public Set<UserRole> getUserRoles() {
    return userRoles;
}

public void setUserRoles(Set<UserRole> userRoles) {
    this.userRoles = userRoles;
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
    Set<GrantedAuthority> grantedAuhority = new HashSet<>();
    for (UserRole userRole : userRoles) {
        grantedAuhority.add(new Authority(userRole.getRole().getName()));
    }
    return grantedAuhority;
}

@Override
public String getPassword() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public String getUsername() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public boolean isAccountNonExpired() {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean isAccountNonLocked() {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean isCredentialsNonExpired() {
    // TODO Auto-generated method stub
    return true;
}


@Override
public boolean isEnabled() {
    // TODO Auto-generated method stub
    return enabled;
}

}

当我把这段代码作为spring boot APP运行时;我得到以下例外情况

null

请帮帮我。

共有2个答案

夔博
2023-03-14

将EaskartApplication.java移动到一个新包,例如com.easkart.app

段宏毅
2023-03-14

将Main移动到root包。这样您的结构看起来就像下面这样。

src/main/java
    -> EaskantApplication (which I guess is the Main class)
        -> All other packages

通常,当主类不位于根时,它往往无法找到所需的资源。

 类似资料:
  • 我有一个带post请求的控制器。我试图用一个简单的NotNull注释验证POJO。我正在使用ControllerAdvice来处理异常。 所以我尝试使用它,但当我启动应用程序时,我得到了以下信息: 因此,我想为BindException创建自己的处理程序,但当我为BindException类创建ExceptionHandler时,spring应用程序不会启动。如果我注释掉handleBindExc

  • 我试图用spring Boot开发一个应用程序。我被困在管理应用程序的异常/错误上。到目前为止,我有服务层和控制器,我已经创建了特定于服务类的异常。对于。(如)异常类是。服务抛出各自的。我被困在如何处理控制器中的异常/错误,如果那里有异常?特别针对api方法调用的格式不正确的输入。我正在读取控制器的输入。我应该把它包括在服务中吗? 所有服务异常都以500个内部应用程序错误的形式返回HTTP代码。我

  • 问题内容: 我正在使用GWT RPC和Hibernate使用Eclipse环境从MySQL插入和检索数据。我已经在服务接口中编写了两种方法来从单个MySQL表插入和检索数据。 该程序运行正常,但引发了此异常。 ServiceImpl类: 入口点类: 问题答案: 您的意思是您的代码工作正常,但您在日志中看到此异常?异常基本上意味着编译缓存由于某种原因无法加载。高速缓存很可能由于某种原因已损坏,因此请

  • 我正在尝试使用intelliJ IDEA创建我的第一个JavaFX程序 我得到这个错误: 这是我的FXML文件: main.java: 这是项目结构: 项目结构 我甚至更改了fxml文件的位置,但他不断得到相同的错误。 FXML文件和Main.java在同一个包中 我是Ubuntu

  • 我正在用VSCode中的C#编写一个语音到文本的模型。模型正确地转录了第一个文件。当我添加新文件并尝试转录时,出现以下错误: 发生异常:CLR/System。ApplicationException类型为“System”的异常。系统中发生ApplicationException“”。私有的CoreLib。dll,但未在Microsoft的用户代码中处理:“异常,错误代码:0x8(SPXERR\u文