Spring Boot REST·不能排除存储库而不排除控制器 [英] Spring Boot REST · Can't exclude a repository without excluding a controller
问题描述
我想从组件扫描中排除一个存储库。但是,我尝试过的四件事情也排除了我不想要的控制器。以下是我的:
Application.java
@SpringBootApplication
@ComponentScan(com.project)
@EntityScan(basePackages = {com.project.model})
@EnableJpaRepositories(com.project.repository)
public class Application extends RepositoryRestConfigurerAdapter implements WebMvcConfigurer {
...
}
SaleItemController .java
@RepositoryRestController
公共类SaleItemController {
...
}
SaleItemRepository.java
@RepositoryRestResource(collectionResourceRel =saleItem,path =saleItems,excerptProjection = SaleItemProjection.class)
public interface SaleItemRepository extends PagingAndSortingRepository< SaleItem,Long> ;, JpaSpecificationExecutor< SaleItem> {}
首次尝试:过滤应用程序
:
@SpringBootApplication
@ComponentScan(com.project)
@EntityScan(basePackages = { com.project.model})
@EnableJpaRepositories(basePackages = {com.project.repository},excludeFilters = {@ ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = SaleItemRepository.class)})
public class Application extends RepositoryRestConfigurerAdapter implements WebMvcConfigurer {
...
}
第二次尝试: SaleItemRepository
上的条件元素:
@RepositoryRestResource (collectionResourceRel =saleItem,path =saleItems,excerptProjection = SaleItemProjection.class)
@ConditionalOnExpression(false)
public interface SaleItemRepository extends PagingAndSortingRepository< SaleItem,Long> ;, JpaSpecificationExecutor< SaleItem> {}
第三次尝试:
@RepositoryRestResource(collectionResourceRel =SaleItem,path =saleItems,excerptProjection = SaleItemProjection.class)
@NoRepositoryBean
public interface SaleItemRepository extends PagingAndSortingRepository< SaleItem,Long> ;, JpaSpecificationExecutor< SaleItem> {}
第四次尝试:在包含过滤器优先于排除过滤器的地方阅读:
@SpringBootApplication
@ComponentScan(basePackages =com.project,includeFilters = {@ ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = SaleItemController.class)})
@EntityScan(basePackages = {com.project.model})
@EnableJpaRepositories(basePackages = {com.project.repository},excludeFilters = {@ ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = SaleItemRepository.class)})
public class Application extends RepositoryRestConfigurerAdapter implements WebMvcConfigurer {
...
}
(将
includeFilters
与excludeFilters $一起放置c $ c>抛出一个异常,因为它找不到另一个仓库)
所有的尝试都成功地排除了仓库,但也排除了我不想要的控制器。我知道存储库已被排除,因为有些内容在被排除时不会被中断,我知道控制器已被排除,因为映射在其中的端点停止工作。
如何仅排除存储库?
预先感谢。
解决方案我终于找到了。它似乎在
SaleItemRepository 的
@RepositoryRestResource $>上使用
exported = false
C $ C>。此外,我删除了excerptProjection = SaleItemProjection.class
。
现在我还有其他的事情需要解决...... p>
I want to exclude a repository from component scan. However, the four things I tried that works also exclude a controller, which I don't want. Here's what I have:
Application.java
@SpringBootApplication @ComponentScan("com.project") @EntityScan(basePackages = { "com.project.model" }) @EnableJpaRepositories("com.project.repository") public class Application extends RepositoryRestConfigurerAdapter implements WebMvcConfigurer { ... }
SaleItemController.java
@RepositoryRestController public class SaleItemController { ... }
SaleItemRepository.java
@RepositoryRestResource(collectionResourceRel = "saleItem", path = "saleItems", excerptProjection = SaleItemProjection.class) public interface SaleItemRepository extends PagingAndSortingRepository<SaleItem, Long>, JpaSpecificationExecutor<SaleItem> {}
First attempt: filters on
Application
:@SpringBootApplication @ComponentScan("com.project") @EntityScan(basePackages = { "com.project.model" }) @EnableJpaRepositories(basePackages = { "com.project.repository" }, excludeFilters={@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value=SaleItemRepository.class)}) public class Application extends RepositoryRestConfigurerAdapter implements WebMvcConfigurer { ... }
Second attempt: conditional element on
SaleItemRepository
:@RepositoryRestResource(collectionResourceRel = "saleItem", path = "saleItems", excerptProjection = SaleItemProjection.class) @ConditionalOnExpression("false") public interface SaleItemRepository extends PagingAndSortingRepository<SaleItem, Long>, JpaSpecificationExecutor<SaleItem> {}
Third attempt:
@RepositoryRestResource(collectionResourceRel = "SaleItem", path = "saleItems", excerptProjection = SaleItemProjection.class) @NoRepositoryBean public interface SaleItemRepository extends PagingAndSortingRepository<SaleItem, Long>, JpaSpecificationExecutor<SaleItem> {}
Fourth attempt: read somewhere that include filters have precedence over exclude filters:
@SpringBootApplication @ComponentScan(basePackages = "com.project", includeFilters={@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value=SaleItemController.class)}) @EntityScan(basePackages = { "com.project.model" }) @EnableJpaRepositories(basePackages = { "com.project.repository" }, excludeFilters={@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value=SaleItemRepository.class)}) public class Application extends RepositoryRestConfigurerAdapter implements WebMvcConfigurer { ... }
(placing the
includeFilters
together withexcludeFilters
throws an exception because it can't find another repository)All attempts successfully exclude the repository, but also exclude the controller, which I don't want. I know the repository has been excluded because some things don't break when it's excluded and I know the controller has been excluded because an endpoint mapped in it stops working.
How do I exclude only the repository?
Thanks in advance.
解决方案I finally found it. It seems to be working with
exported = false
onSaleItemRepository
's@RepositoryRestResource
. Plus, I removedexcerptProjection = SaleItemProjection.class
.Now I have other things to fix...
这篇关于Spring Boot REST·不能排除存储库而不排除控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!