JAX-RS provides the functionality for Representational State Transfer (RESTful) web services. REST is well-suited for basic, ad hoc integration scenarios. Spring MVC offers controllers to create RESTful web services.
In Spring MVC 3.0, we need to explicitly annotate a class with the @Controller
annotation in order to specify a controller servlet and annotate each and every method with @ResponseBody
to serve JSON, XML, or a custom media type. With the advent of the Spring 4.0 @RestController
stereotype annotation, we can combine @ResponseBody
and @Controller
.
The following example will demonstrate the usage of @RestController
:
Create a dynamic web project,
RESTfulWeb
.Modify the
web.xml
file and add a configuration to intercept requests with a SpringDispatcherServlet
:<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java...