Spring WebMVC HTTP OPTIONS Request Handler

Lokesh Gupta

Learn to handle the HTTP OPTIONS requests specific to REST resources in Spring WebMVC applications by adding particular handler methods.

1. What is an HTTP OPTIONS Request?

The HTTP OPTIONS method describes the communication options for the target resource. It allows the client to determine the options and/or requirements associated with a resource, or the capabilities of a server, without implying a resource action or initiating a resource retrieval.

  • If Request-URI is an asterisk (“*”), the OPTIONS request intends to apply to the server in general rather than to a specific resource.
  • If the Request-URI is not an asterisk, the OPTIONS request applies only to the available options when communicating with that resource.
  • Responses to this method are not cacheable.

2. Handling OPTIONS Request with Spring WebMVC

By default, HTTP OPTIONS is handled by setting the Allow response header to the list of HTTP methods listed in all @RequestMapping methods that have matching URL patterns.

We can also explicitly map the @RequestMapping method to HTTP OPTIONS, but that is not necessary in the common case.

@RestController
@RequestMapping(value = "/employee-management", produces = { MediaType.APPLICATION_JSON_VALUE })
public class EmployeeRESTController 
{
   @RequestMapping(value="/employees", method = RequestMethod.OPTIONS)
   ResponseEntity<?> collectionOptions() 
   {
      return ResponseEntity
          .ok()
          .allow(HttpMethod.GET, HttpMethod.POST, HttpMethod.OPTIONS)
              .build();
   }
    
   @RequestMapping(value="/employees/{id}", method = RequestMethod.OPTIONS)
   ResponseEntity<?> singularOptions() 
   {
      return ResponseEntity
          .ok()
          .allow(HttpMethod.GET, HttpMethod.DELETE, HttpMethod.PUT, HttpMethod.OPTIONS)
              .build();
   }
 
   //Other APIs
}

3. Demo

To test whether OPTIONS requests are handled correctly or not, test in any REST client plugin or SoapUI.

Hit the URL: HTTP OPTIONS http://localhost:8080/api/rest/employee-management/employees/1

In cURL, we can test by hitting the following URL. we can see the APIs respond well to OPTIONS requests and provide necessary information about the other possible HTTP methods supported for the requested URI.

curl -i -X OPTIONS http://localhost:8080/SpringRestExample/api/rest/employee-management/employees/
 
Response:
 
#status#    HTTP/1.1 200 OK
Server      Apache-Coyote/1.1
Content-Length  0
Date      Thu, 02 May 2019 10:41:02 GMT
Allow     GET,POST,OPTIONS
curl -i -X OPTIONS http://localhost:8080/SpringRestExample/api/rest/employee-management/employees/1
 
Response:
 
#status#    HTTP/1.1 200 OK
Server      Apache-Coyote/1.1
Content-Length  0
Date      Thu, 02 May 2019 10:43:15 GMT
Allow     GET,DELETE,PUT,OPTIONS

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode