【问题标题】:Spring Boot JPA with REST Service带有 REST 服务的 Spring Boot JPA
【发布时间】:2017-12-15 16:53:51
【问题描述】:

我知道有人问过这样的问题 (Did not find handler method),但在尝试了多种解决方案后,我仍然卡住了。 所以我的问题是:我不能在我的项目中同时使用 REST 和 JPA。 com.db.ruf:WRepository.class:

@NoRepositoryBean
@Component
public interface WRepository <T, ID extends Serializable>
        extends JpaRepository<T, ID> {
}

com.db.ruf:WRepositoryImpl.class:

public class WRepositoryImpl<T, ID extends Serializable>
        extends SimpleJpaRepository<T, ID> implements WRepository<T, ID> {

    private EntityManager entityManager;

    // There are two constructors to choose from, either can be used.
    public WRepositoryImpl(Class<T> domainClass, EntityManager entityManager) {
        super(domainClass, entityManager);

        // This is the recommended method for accessing inherited class dependencies.
        this.entityManager = entityManager;
    }
}

com.db: MyRepositoryFactoryBean.class

public class MyRepositoryFactoryBean <R extends JpaRepository<T, I>, T, I extends Serializable>
        extends JpaRepositoryFactoryBean<R, T, I> {
    /**
     * Creates a new {@link JpaRepositoryFactoryBean} for the given repository interface.
     *
     * @param repositoryInterface must not be {@literal null}.
     */
    public MyRepositoryFactoryBean(Class<? extends R> repositoryInterface) {
        super(repositoryInterface);
    }

    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {

        return new MyRepositoryFactory(entityManager);
    }

    private static class MyRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {

        private EntityManager entityManager;

        public MyRepositoryFactory(EntityManager entityManager) {
            super(entityManager);

            this.entityManager = entityManager;
        }

        protected Object getTargetRepository(RepositoryMetadata metadata) {

            return new WRepositoryImpl<T, I>((Class<T>) metadata.getDomainType(), entityManager);
        }

        protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {

            // The RepositoryMetadata can be safely ignored, it is used by the JpaRepositoryFactory
            //to check for QueryDslJpaRepository's which is out of scope.
            return WRepository.class;
        }
    }
}

com.rest.: WebadminRESTController.class

@RestController
@Component

public class WebadminRESTController {

    @Autowired
   WRepository<ExternalLink, Long> wRepositoryImpl;

    @RequestMapping(value = "/allExternalLinks", method = RequestMethod.GET)
    public ResponseEntity<?> allExternalLinks() {
...
     }
}

com:

@SpringBootApplication
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "com.db.ruf", repositoryFactoryBeanClass = MyRepositoryFactoryBean.class)
@ComponentScan(basePackages = "com", resourcePattern = "com.*")

@EntityScan({"com.db"})


public class WebadminApplication {

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

在这种情况下,我得到:

没有找到 [/allExternalLinks] 的处理方法

如果我改变 @ComponentScan(basePackages = "com", resourcePattern = "com.*") 到 @ComponentScan(basePackages = "com") 或 @ComponentScan 我得到:

原因: org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 符合条件的 bean 类型 'com.db.ruf.WRepository' 可用:预计至少有 1 个符合条件的 bean 作为自动接线候选人。依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我真的不知道怎么了。 谁能这么好心解释一下? 提前谢谢你

【问题讨论】:

  • 维拉,请解释一下,你到底想做什么?使用 data-jpa 和 data-jpa-rest 只需要 3 个类就足够了......你读过这个manual了吗?
  • 问题是我的 entityManager 缓存有问题。当我使用简单的“接口 RepoName 扩展 CrudRepository”调用“保存”时,我得到了不正确的数据,因为保存实体另外被 db 触发器更改。我需要调用刷新或类似的东西。所以根据docs.spring.io/spring-data/jpa/docs/1.4.0.M1/reference/html/…我尝试实现上面的方法。
  • Я создал чат, можем побщаться там...
  • Мне очень жаль, но не выйдет:您必须在 Stack Overflow 上拥有 20 声望才能在此处交谈。

标签: spring rest spring-boot spring-data-jpa spring-rest


【解决方案1】:

WRepositoryImpl.java 上使用@Repository。并编辑WebadminRESTController.java

@Autowired WRepositoryImpl<ExternalLink, Long> wRepositoryImpl;

【讨论】:

  • 当我这样做时,我得到:> org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“java.lang.Class>”类型的合格bean:预计至少有1个合格的bean作为自动接线候选人。依赖注释:{}
  • 在您的 WRepositoryImpl 类上尝试@Service,并从您的界面中删除@Component。在您的 WebadminRESTController 类中,将指定的行编辑为 @Autowired WRepositoryImpl&lt;ExternalLink, Long&gt; wRepositoryImpl;
  • 抱歉,试试@Repository
  • > org.springframework.beans.factory.NoSuchBeanDefinitionExcept‌​ion:没有可用的“java.lang.Class>”类型的合格bean:预计至少有1个有资格作为自动装配候选者的bean。依赖注解:{} .....(
【解决方案2】:

无意中找到了解决方案(不要认为它是最好的,但至少它有效):

public interface  ExternalLinksRepo extends  CrudRepository<ExternalLink, Long> {
...
}

和:

public class WebadminRESTController {

   @Autowired
   ExternalLinksRepo wRepositoryImpl;
...}

然后 wRepositoryImpl 被自动创建为 WRepositoryImpl 的实例

感谢所有人,尤其是 Cepr0

【讨论】:

    猜你喜欢
    • 2015-08-16
    • 1970-01-01
    • 2021-08-28
    • 2021-11-09
    • 2018-05-12
    • 2017-01-10
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    相关资源
    最近更新 更多