I know that such questions were asked ( Did not find handler method ), but after trying multiple solutions I'm still stucked. So my problem is: I can't use both REST and JPA in my project. 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);
}
}
In this case I get:
Did not find handler method for [/allExternalLinks]
If I change @ComponentScan(basePackages = "com", resourcePattern = "com.*") to @ComponentScan(basePackages = "com") or @ComponentScan I get:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.db.ruf.WRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
I really don't know what's wrong. Could anyone be so kind to explain it? Thank you in advance
Use @Repository
on WRepositoryImpl.java
. And edit WebadminRESTController.java
:
@Autowired WRepositoryImpl<ExternalLink, Long> wRepositoryImpl;
Accidentally I found solution (don't think that it is the best one, but at least it works):
public interface ExternalLinksRepo extends CrudRepository<ExternalLink, Long> {
...
}
and:
public class WebadminRESTController {
@Autowired
ExternalLinksRepo wRepositoryImpl;
...}
Then wRepositoryImpl is automatically created as instance of WRepositoryImpl
Thanks to all, especially to Cepr0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.