Spring RESTful Services - Interview Questions

What annotation is used in Spring to make a class handle HTTP requests?

 

Spring builds RESTful web services through a class that handles HTTP requests. This class is called the controller class. Controller classes are identified by either the @Controller annotation or the @RestController annotation. In below example OrderController class is applied the @RestController annotation to make it a Spring RESTful web service controller.

@RestController
public class OrderController {
 ...
}

What annotation is used in Spring to map a HTTP request url to a controller method?

 

@RequestMapping is used to map a specific HTTP url to a controller method. In below example the method getOrder() handles the '/order' url request.

@RestController
public class OrderController {
 @RequestMapping('/order')
 public Order getOrder() {
  //return order;
 }
}

How do you limit a Spring controller method to handle only HTTP GET operation requests?

 

You can limit a Spring controller method to handle only HTTP GET operation requests by using parameter 'method=GET' in @RequestMapping annotation.

Alternatively you can use the @GetMapping annotation.

In below examples the method getOrder() handles the '/order' url request for HTTP GET operations only.

@RestController
public class OrderController {
 @RequestMapping(value='/order',method=GET)
 public Order getOrder() {
  //return order;
 }
}
@RestController
public class OrderController {
 @GetMapping(value='/order')
 public Order getOrder() {
  //return order;
 }
}

How do you bind HTTP request query string parameters to a controller method parameters?

 

The annotation @RequestParam is used to bind HTTP request query string parameters to controller method parameters.

In below example query string parameter 'orderId' is mapped to method parameter 'orderNumber'.

@RestController
public class OrderController {
 @RequestMapping(value='/order',method=GET)
 public Order getOrder(@RequestParameter(value='orderId') Integer orderNumber) {
  //return order;
 }
}

How do you bind HTTP request URI parameters to a controller method parameters?

 

The annotation @pathVariable is used to bind HTTP uri parameters to controller method parameters. In below example URI parameter 'orderId' is mapped to method parameter 'orderNumber'.

@RestController
public class OrderController {
 @RequestMapping(value='/order/{orderId}',method=GET)
 public Order getOrder(@PathVariable(value='orderId') Integer orderNumber) {
  //return order;
 }
}

How do you limit a Spring controller method to handle only HTTP POST operation requests?

 

You can limit a Spring controller method to handle only HTTP POST operation requests by using parameter 'method=POST' in @RequestMapping annotation.

Alternatively you can use the @PostMapping annotation.

In below examples the method newOrder() handles the '/order' url request for HTTP POST operations only.

@RestController
public class OrderController {
 @RequestMapping(value='/order',method=POST)
 public Order newOrder() {
  //return order;
 }
}
@RestController
public class OrderController {
 @PostMapping(value='/order')
 public Order newOrder() {
  //return order;
 }
}

How do you limit a Spring controller method to handle only HTTP PUT operation requests?

 

You can limit a Spring controller method to handle only HTTP PUT operation requests by using parameter 'method=PUT' in @RequestMapping.

Alternatively you can use the @PutMapping annotation.

In below example the method Order() handles the '/order' url request for HTTP PUT operations only.

@RestController
public class OrderController {
 @RequestMapping(value='/order',method=PUT)
 public Order newOrder() {
  //return order;
 }
}
@RestController
public class OrderController {
 @PutMapping(value='/order')
 public Order newOrder() {
  //return order;
 }
}

How do you bind the request body of HTTP POST or HTTP PUT request to an object?

 

The annotation @RequestBody is used to bind the request body of an HTTP POST or HTTP PUT request to an object. In below example the request body is mapped to the object 'Order'.

@RestController
public class OrderController {
 @RequestMapping(value='/order/{orderId}',method=PUT)
 public Order addOrder(@RequestBody Order order) {
  //return order;
 }
}

How do you bind an object to the HTTP Response of a HTTP Request?

 

The annotation @ResponseBody is used to bind an object to the HttpResponse.

On a controller having @Controller annotation a method has to be specifically annotated with @ResponseBody annotation in order to bind the returned object to the HttpResponse object.

On a controller having @RestController annotation a method need not be specifically annotated with @ResponseBody annotation since it is added by default.

Below examples show the handling of @ResponseBody annotation for @Controller annotated controller and @RestController annotated controller.

@RestController
public class OrderController {
 @RequestMapping(value='/order/{orderId}',method=PUT)
 public Order addOrder(@RequestBody Order order) {
  //return order;
 }
}
@Controller
public class OrderController {
 @RequestMapping(value='/order/{orderId}',method=PUT)
 @ResponseMapping
 public Order addOrder(@RequestBody Order order) {
  //return order;
 }
}

What is the difference between @ResponseBody and @ResponseEntity annotations?

 

@ResponseEntity represents an HTTP Response object including headers, response body and status. If you want to add headers to the response in addion to the body and status then use @ResponseEntity annotation.

@ResponseBody annotation represents only the response body of an HTTP Response object. When you use the annotation @ResponseBody on a controller method, the object returned from this method is bound to the response body of the HTTP Response object that is returned back to client.

 
Subscribe to our Questions

 

Spring - Interview Questions

Spring FrameworkSpring DataSpring Data JDBCSpring Data MongoDBSpring Web FlowSpring WebFluxSpring RESTful ServicesSpring Security
 
RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example