Spring Boot MVC and Rest

MVC Applications

There are two types of MVC Applications:

Web Servlet
Based on Servlets
Uses Servlets, Filters, Listeners
Web Reactive
Non-blocking approach
Supports reactive programming

To use an War the Application class must extend from SpringBootServletInitializer.

Restful services

Principles for Rest:

  • expose resources through url’s
  • resources support limited set of operations
  • clients can request an particular representation
  • representations can link to others resources
  • should be stateless

Crud operations

The components of the Request and Response are:

Request

  • Method (also called Verb)
  • URI (also called Endpoint)
  • Body

Response

  • Status Code
  • Body

If you want to go into more depth around Request and Response methods, check out the HTTP standard.

The power of REST lies in the way it references a Resource, and what the Request and Response look like for each CRUD operation. Let’s take a look at what our API will look like when we’re done with this course:

  • For CREATE: use HTTP method POST.
  • For READ: use HTTP method GET.
  • For UPDATE: use HTTP method PUT.
  • For DELETE: use HTTP method DELETE.

Post vs Put

Controllers

@ResponseBody disables the View handling system.

@Controller
public class AccountController {
 @GetMapping("/accounts")
 public @ResponseBody List<Account> list(){
 ..
 }
}

@RestController annotation incorporates @Controller and @ResponseBody, assuming that Rest response data is returned.

Spring will automatically inject argument in the method endpoint: HttpServletRequest, HttpSession, Principal, Locale.

Method examples

Message converters

Response message format can be leverage with ResponseEntity by setting headers or content type.