繁体   English   中英

具有REST服务的Spring Boot JPA

[英]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)}

我真的不知道怎么了 有人能解释吗? 先感谢您

使用@RepositoryWRepositoryImpl.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM