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
@EnableWebMvcunless 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
@RestControlleror@Controller, and their package is a subpackage of your main@SpringBootApplicationclass. If you added a custom@ComponentScanto your Shiro config, make sure it doesn’t exclude your controller packages. - Enable debug logging: Add
logging.level.org.springframework.web=DEBUGtoapplication.propertiesto 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
@EnableJpaRepositoriesor@ComponentScan, make sure theirbasePackagesdon’t only target JPA repositories/entities. For example,@EnableJpaRepositories(basePackages = "com.yourproject.repos")is fine (it only scans repos), but if you added a@ComponentScanthat 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 implementingWebMvcConfigurerinstead—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=DEBUGto see if your controller beans are being created. If not, use@Lazyon the problematic dependency to break the cycle. - Explicitly define scan ranges (if needed): Add
scanBasePackagesto your main@SpringBootApplicationannotation 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
相关产品推荐
相关产品推荐

