简体   繁体   中英

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

@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 with excludeFilters 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 on SaleItemRepository 's @RepositoryRestResource . Plus, I removed excerptProjection = SaleItemProjection.class .

Now I have other things to fix...

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.

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