1. Use of OPTIONS method in Restful?

In REST, OPTIONS is a method level annotation, this annotation indicates that the following method will respond to the HTTP OPTIONS request only. It is used to request, for information about the communication option available for a resource.

This method allows the client of the REST API to determine, which HTTP method ( GET, HEAD, POST, PUT, DELETE ) can be used for a resource identified by requested URI, without initiating a resource request by using any particular HTTP method.

Response to this method are not cacheable.

200 OK response should include any header fields like Allow, that determine the communication option implemented by the server and are applicable for a resource identified by requested URI.

The response body ( if any ) should also include information about the communication option available for a resource. The format for such a body is not defined by the specification, but might be defined by future extensions to HTTP.

If response body is not included then value of Content-Length header field should be 0.

 Example :-
200 OK
Allow: HEAD,GET,PUT,DELETE,OPTIONS

If request URI is an asterisk sign ( * ), then the OPTIONS method request applies to the server rather than to a specific resource. It can be used as a ping to server for checking its capabilities.

REST OPTIONS method is also used for CORS ( Cross-Origin Resource Sharing ) request.

2. What is the difference between @Controller and @RestController?

@RestController = @Controller + @ResponseBody

The response from the @RestController are directly returned as a response after conversion to JSON or XML.

In Summary

  • @Controller : Uses a view resolver to find the view. You are returning either the View, View Name or Model and View.
  • @RestController : You are returning a bean. The bean would be converted to a JSON using a Jackson message converter.

3. HttpRequest scope vs Prototype scope ?

Prototype creates a brand new instance every-time you call getBean on the ApplicationContext. Whereas for Request, only one instance is created for an HttpRequest. So in a single HttpRequest, I can call getBean twice on Application and there will only ever be one bean instantiated, whereas that same bean scoped to Prototype in that same single HttpRequest would get 2 different instances.

  • HttpRequest scope
Mark mark1 = context.getBean("mark"); 
Mark mark2 = context.getBean("mark"); 
mark1 == mark2; //This will return true 
  • Prototype scope

Mark mark1 = context.getBean("mark");
Mark mark2 = context.getBean("mark");
mark1
== mark2; //This will return false

Why do we use @ResponseBody sometimes and ResponseEntity some other times?

  • You use ResponseEntity when you want to customize the Response Status.
  • In all other situations, we use @ResponseBody.
  • Example 1
    @GetMapping(value=”/resource”)
    @ResponseBody
    public Resource sayHello()
    { return resource; }
  • Example 2
    @PostMapping(value=”/resource”)
    public ResponseEntity createResource()
    { …. ….
    return ResponseEntity.created(resource).build();
    }
  • There are a variety of HTTP Response Status Codes that you can return with your response.
  • 200 – SUCCESS
    201 – CREATED
    404 – RESOURCE NOT FOUND
    400 – BAD REQUEST
    401 – UNAUTHORIZED
    500 – SERVER ERROR
  • With @ResponseBody the only response type that goes out is SUCCESS (Example 1). (or 500 if a server error happens)
  • Let’s say you are creating a resource. You would want to send a status of 201 CREATED. In this case, we use ResponseEntity as in Example 2.

What is the difference between ModelMap and ModelAndView?

  • Model is an interface while ModelMap is a class.
  • ModelAndView is just a container for both a ModelMap and a View object.
  • ModelAndView allows a controller to return both as a single value.
  • usually you will have ModelAndView to return the model and view from a controller.
  • However, there is an option where you can just add values to ModelMap and return viewname from the controller method
  • http://stackoverflow.com/questions/16951609/when-to-use-modelandview-vs-model-in-spring

Can I have multiple parameters with the same name in a request?
ie: say http://localhost:8080/login?name=john&name=peter&name=dona

Yes,

  • You can accept all values using an array in the controller method.

Here’s a snippet!

public String method(@RequestParam(value="name") String[] names){
 
    //if length of names is not 1, throw an error!
 
    //Continue processing.
 
}