Study Notes of Spring
Annotation
@GetMapping
The @GetMapping
annotation ensures that HTTP GET requests to /greeting
are mapped to the greeting()
method.
There are companion annotations for other HTTP verbs (e.g. @PostMapping
for POST
). There is also a @RequestMapping
annotation that they all derive from, and can serve as a synonym (e.g. @RequestMapping(method=GET)
).
@RequestParam
@RequestParam
binds the value of the query string parameter name
into the name
parameter of the greeting()
method. If the name
parameter is absent in the request, the defaultValue
of World
is used.
@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
@RestController
Spring @RestController
marks the class as a controller where every method returns a domain object instead of a view. It is shorthand for including both @Controller
and @ResponseBody
.
A key difference between a traditional MVC controller and the RESTful web service controller shown earlier is the way that the HTTP response body is created. Rather than relying on a view technology to perform server-side rendering of the greeting data to HTML, this RESTful web service controller populates and returns a domain object. The object data will be written directly to the HTTP response as JSON.
@ResponseBody
The @ResponseBody
annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse
object.
@SpringBootApplication
@SpringBootApplication
is a convenience annotation that adds all of the following:
@Configuration
: Tags the class as a source of bean definitions for the application context.
@EnableAutoConfiguration
: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. For example, if spring-webmvc
is on the classpath, this annotation flags the application as a web application and activates key behaviors, such as setting up a DispatcherServlet
.
@ComponentScan
: Tells Spring to look for other components, configurations, and services in the com/example
package, letting it find the controllers.
@Bean
Bean
is a method-level annotation and a direct analog of the XML element. The annotation supports most of the attributes offered by , such as: init-method, destroy-method, autowiring, lazy-init, dependency-check, depends-on and scope.
@Repository
Spring @Repository
annotation is used to indicate that the class provides the mechanism for storage, retrieval, search, update and delete operation on objects.
Spring Repository annotation is a specialization of @Component annotation, so Spring Repository classes are autodetected by spring framework through classpath scanning.
Spring Repository is very close to DAO pattern where DAO classes are responsible for providing CRUD operations on database tables. However, if you are using Spring Data for managing database operations, then you should use Spring Data Repository interface.
This article introduces an example of Spring Repository annotation.