[英]Spring Boot JPA with REST Service
我知道有人問過這樣的問題( 未找到處理程序方法 ),但是在嘗試了多種解決方案后,我仍然陷入困境。 所以我的問題是:我不能在項目中同時使用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:沒有類型為'com.db.ruf.WRepository'的合格Bean:期望至少有1個合格為自動裝配候選的Bean。 依賴項注釋:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
我真的不知道怎么了 有人能解釋嗎? 先感謝您
使用@Repository
對WRepositoryImpl.java
。 並編輯WebadminRESTController.java
:
@Autowired WRepositoryImpl<ExternalLink, Long> wRepositoryImpl;
偶然地我找到了解決方案(不要認為這是最好的解決方案,但至少可以奏效):
public interface ExternalLinksRepo extends CrudRepository<ExternalLink, Long> {
...
}
和:
public class WebadminRESTController {
@Autowired
ExternalLinksRepo wRepositoryImpl;
...}
然后自動創建wRepositoryImpl作為WRepositoryImpl的實例
感謝所有人,尤其是Cepr0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.