0

角色存储库

package com.jawad.api.repository;

import javax.management.relation.Role;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RoleRepository extends CrudRepository<Role,Integer>{

}

应用控制器

package com.jawad.api.controller;

import java.util.List;

import javax.management.relation.Role;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jawad.api.repository.RoleRepository;

@RestController
public class AppsController {
    
    @Autowired
    private RoleRepository rolerepository;
    
    @GetMapping("/getAllRoles")
    public List<Role> getAllRoles() {
        return (List<Role>) rolerepository.findAll();
    }
    
    @GetMapping("/hi")
    public String hi(){
        return "hi";
    }
}

PrepaidWebAppApplication

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class PrepaidWebAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(PrepaidWebAppApplication.class, args);
    }    
}

当我运行我的应用程序时,我得到一个错误,我找不到修复它的方法:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Not a managed type: class javax.management.relation.Role
        at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:176) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:101) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1827) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getObjectForBeanInstance(AbstractAutowireCapableBeanFactory.java:1265) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:334) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:624) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:612) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.data.repository.config.DeferredRepositoryInitializationListener.onApplicationEvent(DeferredRepositoryInitializationListener.java:51) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.repository.config.DeferredRepositoryInitializationListener.onApplicationEvent(DeferredRepositoryInitializationListener.java:36) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:898) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at com.jawad.api.PrepaidWebAppApplication.main(PrepaidWebAppApplication.java:12) [classes/:na]
    Caused by: java.lang.IllegalArgumentException: Not a managed type: class javax.management.relation.Role
        at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:582) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
        at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:85) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
        at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:75) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:66) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:229) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:179) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:162) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:72) ~[spring-data-jpa-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:309) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:297) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.util.Lazy.getNullable(Lazy.java:212) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.util.Lazy.get(Lazy.java:94) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:244) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:57) ~[spring-data-commons-2.3.2.RELEASE.jar:2.3.2.RELEASE]
        at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:169) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
        ... 24 common frames omitted

当我移动PrepaidWebAppApplication.java到包含 3 个模型(用户、角色和机构)的模型文件夹时,它可以工作,但是我的获取请求将不再被识别。我得到错误:404 not found

我该如何解决这个问题?

4

3 回答 3

1

你已经从你的包中导入了Role实体and 。从您的项目中导入它,它应该适合您。javaxRoleRepositoryAppsController

import javax.management.relation.Role;

将上述导入更改为您的项目Role实体。

于 2020-08-13T14:04:36.333 回答
1

一些基本概念spring-boot-starter-data-jpa

  1. stereotype注释,如:
    A. @Component
    B. @Controller
    C. @Service
    D. spring 中的 @Repository
    必须是具体类。

  2. 最新版本的 Spring Boot 已@EnableJpaRepositories自动启用,依赖spring-boot-starter-data-jpa.

  3. @EnableJpaRepository负责 , , 等接口实现的CrudRepository注册JPARepository。这里我们不需要stereotype添加SpringFramework.

  4. 对于 interface CrudRepositoryJPARepository它具有 JPA 的一部分,因此它所引用的类必须具有 JPA 注释,例如@Entity@Table在具体类上。

现在回到你的问题陈述

对于import javax.management.relation.Role

  1. import javax.management.relation.Role是一个具体类,即不是抽象类 -->接受
  2. import javax.management.relation.Role类没有JPA注解,所以不能直接用 with CrudRepository, JPARepository---> Not Accepted
  3. RoleRepository不应该有stereotype注释。

解决方案:

  1. 创建一个类似于import javax.management.relation.Role在其上使用 JPA 注释的类。
  2. 中使用的 JPA 实体CrudRepository,其中JPARepository必须有@Id字段,否则您将遇到新的异常。
于 2020-08-13T15:29:25.633 回答
-1

还请交叉检查您的实体类,我错过了下面的 Id 注释,以及主键上生成的值注释,由于存储库上发生了 org.springframework.beans.factory.BeanCreationException。

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
于 2020-12-28T03:06:33.127 回答