【发布时间】: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