In the last tutorial, we learned the purpose of overriding WebMvcConfigurer#addViewControllers (ViewControllerRegistry). In this example, We will map a URL to a view name along with a response status code. We'll also see how to register a status code that is returned along with an empty response body.
Example
In this example we are creating two view controllers:
- Mapping a URL to a view and setting a status code.
- Returning a status code without a message body
package com.logicbig.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//mapping url to a view
ViewControllerRegistration r =
registry.addViewController("/test");
r.setViewName("myView");
//setting status code
r.setStatusCode(HttpStatus.GONE);
//mapping another url to a status code
registry.addStatusController("/test2",
HttpStatus.SERVICE_UNAVAILABLE);
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/pages/", ".jsp");
}
}
src/main/webapp/WEB-INF/pages/myView.jsp<html>
<body style="margin:20px;">
JSP view
<p>
</p>
<p>
Response Status: <%=response.getStatus() %>
</p>
</body>
</html>
Running application:
To try examples, run embedded Jetty (configured in pom.xml of example project below):
mvn jetty:run
$ curl -s "http://localhost:8080/test" <html> <body style="margin:20px;"> JSP view <p>
</p> <p> Response Status: 410 </p> </body> </html>
$ curl -si "http://localhost:8080/test2" HTTP/1.1 503 Service Unavailable Date: Sun, 27 Sep 2026 06:03:43 GMT Content-Length: 0 Server: Jetty(9.4.53.v20231009)
Note that as of Spring Framework 6.0, both ViewControllerRegistration#setStatusCode and ViewControllerRegistry#addStatusController accept an HttpStatusCode parameter instead of HttpStatus. Since HttpStatus implements HttpStatusCode, existing code that passes an HttpStatus constant, such as HttpStatus.SERVICE_UNAVAILABLE above, still compiles and works without any changes.
In the next tutorial, we'll see how to redirect a URL to another URL using ViewControllerRegistry.
Example ProjectDependencies and Technologies Used: - spring-webmvc 7.0.6 (Spring Web MVC)
Version Compatibility: 4.1.0.RELEASE - 7.0.6 Version compatibilities of spring-webmvc with this example: Versions in green have been tested.
- jakarta.servlet-api 6.1.0 (Jakarta Servlet API documentation)
- JDK 25
- Maven 3.9.11
|