ViewResolver in Spring MVC — Resolving View Names to Templates
Fast recall
Quick revision
Core answer
Understanding of how traditional @Controller view resolution works for server-side HTML rendering
Thinking ViewResolver is used in REST APIs — it's only for @Controller methods returning view names, not @RestController returning JSON
Mention ContentNegotiatingViewResolver for multiple view types, and explain that @RestController bypasses ViewResolver entirely because @ResponseBody is implicit
Interview-ready guide
Interview answer
A method can add model.addAttribute("users", users) and return "users". The resolver supplies the physical template and the view renders it. The exact prefix/suffix belongs to the selected view technology and configuration. Returning the literal String from a @RestController instead writes it as body text; it does not resolve the template.
ViewResolver in Spring MVC — Resolving View Names to Templates
Direct Answer
A ViewResolver turns a logical view name returned by a view-oriented @Controller into a View implementation. An InternalResourceViewResolver can map users to /WEB-INF/views/users.jsp; a Thymeleaf resolver can map it to a template. Response-body methods normally use HttpMessageConverters instead, although Spring MVC can be configured with other view/negotiation arrangements.
Code Example
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public String listUsers(Model model) {
var users = userService.findAll();
model.addAttribute("userList", users);
return "users"; // logical view name
}
}
// application.properties
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
// Spring Boot auto-configures InternalResourceViewResolver
// Resolves "users" → /WEB-INF/views/users.jsp
// The JSP template accesses ${userList} to render the HTML table
Common Mistakes & Pitfalls
- Using @Controller instead of @RestController for REST APIs: Freshers add ViewResolver config to a REST API project, then wonder why they get 404 errors. If you're returning JSON, use @RestController which has implicit @ResponseBody on every method — ViewResolver is never called.
- Forgetting template files in the classpath: You configure ThymeleafViewResolver but put templates in src/main/webapp instead of src/main/resources/templates. Spring Boot's default resolver looks in classpath:templates/, not webapp.
- Returning view names from @RestController methods: If you accidentally use @RestController and return a String, Spring treats it as the response body (literal string), not a view name. You'll see "userList" as plain text in the browser instead of rendered HTML.
Check your understanding
Answer these without looking back at the article.
- Does @RestController use ViewResolver?
- How do you configure Thymeleaf as the view resolver in Spring Boot?
- What is ContentNegotiatingViewResolver?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.