Spring Boot中@Controller失效但@RestController正常的问题排查
Hey there, let's figure out why your JSP isn't loading and fix this step by step! The 404 error tells us Spring can't resolve your view, even though your controller is routing correctly (since @RestController returns the string). Here's what's going wrong and how to fix it:
1. You're Missing the JSP Rendering Dependency
Spring Boot's spring-boot-starter-web doesn't include the libraries needed to compile and render JSPs by default. You need to add the Tomcat JSP engine dependency to your pom.xml:
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
The provided scope works because the embedded Tomcat will handle it at runtime—no extra setup needed for local development.
2. Jersey and Spring MVC Are Conflicting
You've added both spring-boot-starter-jersey (a JAX-RS framework) and spring-boot-starter-web (Spring MVC) to your dependencies. These two frameworks fight over handling web requests, which messes up Spring's view resolution.
Unless you specifically need Jersey for JAX-RS endpoints, remove this dependency entirely:
<!-- Delete this block from your pom.xml --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency>
3. Double-Check Your Directory Structure
Make sure your JSP is in the right place. With your current application.properties config (spring.mvc.view.prefix=/WEB-INF/views and suffix=.jsp), your project structure should look like this:
src/ └── main/ ├── java/ │ └── com/demo/CrudOperations/ │ └── RegistrationController.java └── webapp/ └── WEB-INF/ └── views/ └── registration.jsp
If your JSP is in src/main/resources or another folder, Spring won't find it—move it to src/main/webapp/WEB-INF/views/.
4. Ditch the Snapshot Spring Boot Version
You're using 2.5.0-SNAPSHOT, which is an unstable pre-release version. These can have bugs that break view resolution. Switch to a stable release, like the LTS version for 2.5.x:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.14</version> <!-- Stable LTS build --> <relativePath /> </parent>
You can also remove the snapshot/milestone repositories from your pom—they're only needed for pre-release builds.
5. Keep Your Controller as @Controller (No @ResponseBody!)
Your original controller code is correct—don't add @ResponseBody! That annotation tells Spring to return the string directly instead of resolving it to a JSP view. Keep it exactly as you had:
package com.demo.CrudOperations; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class RegistrationController { @RequestMapping(value = "/welcome", method= RequestMethod.GET ) public String register(){ return "registration"; // Spring will map this to /WEB-INF/views/registration.jsp } }
Once you've made all these changes, rebuild your project and restart the app. Visiting http://localhost:8080/welcome should now load your JSP page instead of showing a 404.
内容的提问来源于stack exchange,提问作者Pravin Kottawar

