我知道问了这些问题(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 =&#34; com&#34;,resourcePattern =&#34; com。*&#34;)到@ComponentScan(basePackages =&#34; com&#34;)或@ComponentScan我得到:< / p>
引起: org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 合格的bean类型 &#39; com.db.ruf.WRepository&#39;可用:预计至少有1个符合条件的bean 作为autowire候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}
我真的不知道出了什么问题。 任何人都可以这么好解释吗? 提前谢谢
答案 0 :(得分:0)
在@Repository
上使用WRepositoryImpl.java
。并修改WebadminRESTController.java
:
@Autowired
WRepositoryImpl<ExternalLink, Long> wRepositoryImpl;
答案 1 :(得分:0)
无意中我找到了解决方案(不要认为它是最好的解决方案,但至少它有效):
Func<Task> func
和
public interface ExternalLinksRepo extends CrudRepository<ExternalLink, Long> {
...
}
然后wRepositoryImpl自动创建为WRepositoryImpl的实例
感谢所有人,特别是对Cepr0