简体   繁体   English

Spring Boot REST·如果不排除控制器,则无法排除存储库

[英]Spring Boot REST · Can't exclude a repository without excluding a controller

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 SaleItemController.java

@RepositoryRestController
public class SaleItemController {
    ...
}

SaleItemRepository.java SaleItemRepository.java

@RepositoryRestResource(collectionResourceRel = "saleItem", path = "saleItems", excerptProjection = SaleItemProjection.class)
public interface SaleItemRepository extends PagingAndSortingRepository<SaleItem, Long>, JpaSpecificationExecutor<SaleItem> {}

First attempt: filters on Application : 首次尝试:在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 : 第二次尝试: 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 with excludeFilters throws an exception because it can't find another repository) (将includeFiltersexcludeFilters一起放置会引发异常,因为它找不到其他存储库)

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 on SaleItemRepository 's @RepositoryRestResource . 这似乎与合作exported = falseSaleItemRepository@RepositoryRestResource Plus, I removed excerptProjection = SaleItemProjection.class . 另外,我删除了excerptProjection = SaleItemProjection.class

Now I have other things to fix... 现在我还有其他要解决的问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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