You get the response in JSON because of @RestController, How??
@RestController
is itself annotated with @Controller
and
@ResponseBody
implying that @RequestMapping
methods assume
@ResponseBody
semantics by default.You can have more information here.
To be more comprehensive, your code will look like this :
@RequestMapping(value = "/topmoviesWS", method = RequestMethod.GET,
headers="Accept=application/json")
public @ResponseBody List<?> getTopMoviesWS() {
return l1;
}
- the
@ResponseBody
annotation on the getTopMoviesWS()
method tells Spring MVC that it does not need to render the response through a server-side view layer, but that instead the list returned is the response body, and should be written out directly to the HTTP response as JSON.
The list returned must be converted to JSON, you don’t need to do this conversion manually. Because Jackson 2 is on the classpath, Spring’s MappingJackson2HttpMessageConverter
is automatically chosen to convert the values to JSON.
You can have a look here to understand the @ResponseBody
mechanism.