Spring – Annotations

In this blog I will try to explain most commonly used annotations in Spring.

1. @Component

2. @Service

@Service is stereotype annotation which tells Spring that it is a Business Service class. Spring registers @RESTController as Controllers likewise classes annotated with @Service are also be registered as Business Services.

3. @Repository

4. @Controller

5. @RESTController

@RESTController annotation is applied over classes. It is used to make any Java class a REST Controller. The @RESTController annotation in Spring MVC is nothing but the combination of @Controller and @ResponseBody annotation.

package org.techlearnings.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController("/users")
public class UserController {

	@RequestMapping(value="/greeting")
	public String greetings() {
		return "Hi";
	}
}

6. @RequestMapping

@RequestMapping annotation is applied over methods. It allows you to execute the method when HTTP request with specific URL is made.

package org.techlearnings.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController("/users")
public class UserController {

	@RequestMapping(value="/greeting")
	public String greetings() {
		return "Hi";
	}
}

Note: @RequestMapping annotation maps only the GET method by default. In other words, By default, method annotated with @RequestMapping, handles GET requests. To map to other HTTP methods, you will need to specify method type in @RequestMapping annotation.

6.1 Properties of @RequestMapping Annotation

6.1.1 value
6.1.2 method
6.1.3 params
6.1.4 produces
6.1.5 consumes
6.1.6 headers
6.1.7 name
6.1.8 path

7. @RequestBody

@RequestBody annotation is used inside method parameters. It tells Spring MVC that Request Payload contains JSON representation of particular Java object. By declaring @RequestBody annotation we are asking Spring MVC to take that JSON representation and convert that into Java object and pass that to method.

Example

@RequestMapping(method=RequestMethod.POST, value="/user")
public void greetings(@RequestBody User user) {
	//TODO
}

8. @PathVariable

@PathVariable is used to map variable argument passed in Request URI to method argument.

Example

@RequestMapping(value="/users/{id}")
public void greetings(@PathVariable String id) {
	//TODO
}

If your Request URI variable name and method argument name are:
Same – then Spring MVC automatically maps those (As in code above).
Different – then you can map those by defining @PathVariable property (As in code below).

Example

@RequestMapping(value="/users/{id}")
public void greetings(@PathVariable("id") String userId) {
	//TODO
}

9. @PathParam

Author: Mahesh

Technical Lead with 10 plus years of experience in developing web applications using Java/J2EE and web technologies. Strong in design and integration problem solving skills. Ability to learn, unlearn and relearn with strong written and verbal communications.