You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot集成Shiro/JPA后控制器映射失效问题求助

Hey Felix, let's work through these two frustrating Spring Boot issues one by one—they’re both tied to auto-configuration or component scanning quirks that pop up when adding new dependencies.

Issue 1: No Request Mappings Found After Configuring Apache Shiro

This usually happens because Shiro’s configuration is either blocking Spring MVC’s mapping registration or interfering with component scanning. Here’s how to fix it:

  • Double-check your Shiro config class: Don’t add @EnableWebMvc unless you intentionally want to override Spring Boot’s default MVC auto-configuration. That annotation replaces Spring Boot’s pre-configured MVC setup, which often breaks request mapping scanning.
  • Fix your Shiro filter chain rules: If your filter chain is set to intercept all requests (like /** = authc), Spring MVC’s endpoints will get blocked before they even register. Add a rule to let unauthenticated requests reach your controllers, e.g.:
    filterChainDefinitionMap.put("/your-controller-path/**", "anon");
    
  • Verify component scanning: Ensure your controllers are annotated with @RestController or @Controller, and their package is a subpackage of your main @SpringBootApplication class. If you added a custom @ComponentScan to your Shiro config, make sure it doesn’t exclude your controller packages.
  • Enable debug logging: Add logging.level.org.springframework.web=DEBUG to application.properties to see if Spring MVC is actually registering your mappings. If the logs show mappings being registered but requests still fail, the problem is definitely Shiro’s filter blocking them.

Issue 2: Controller Mappings Fail After Adding spring-boot-starter-data-jpa

Even though your controllers are in the main class’s subpackage, JPA’s auto-configuration can subtly alter component scanning or bean initialization order. Try these fixes:

  • Avoid over-restricting component scan ranges: If you manually added @EnableJpaRepositories or @ComponentScan, make sure their basePackages don’t only target JPA repositories/entities. For example, @EnableJpaRepositories(basePackages = "com.yourproject.repos") is fine (it only scans repos), but if you added a @ComponentScan that only includes repo packages, your controllers won’t be picked up.
  • Replace WebMvcConfigurationSupport with WebMvcConfigurer: If you have a custom MVC config using WebMvcConfigurationSupport, it disables Spring Boot’s auto-MVC config, which can clash with JPA’s auto-config. Switch to implementing WebMvcConfigurer instead—it lets you customize MVC without overriding everything.
  • Check for silent bean initialization failures: Even if the startup logs look normal, a circular dependency between a controller and JPA repository might prevent the controller from being registered. Add logging.level.org.springframework.context.annotation=DEBUG to see if your controller beans are being created. If not, use @Lazy on the problematic dependency to break the cycle.
  • Explicitly define scan ranges (if needed): Add scanBasePackages to your main @SpringBootApplication annotation to ensure all critical packages are scanned:
    @SpringBootApplication(scanBasePackages = "com.yourproject")
    
  • Verify EntityScan configuration: If you added @EntityScan, make sure it only targets your entity packages—don’t include controller packages here, as it can interfere with normal component scanning.

内容的提问来源于stack exchange,提问作者Felix Klemm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:08:01