Implementing the REST service

Let's start by creating a simple REST controller as follows:

package com.dineshonjava.masteringspringboot.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

   @GetMapping("/hello")
   String sayHello(){
         return "Hello World!!!";
   }
} 

Let's see this small REST controller (HelloController) in detail:

  • @RestController annotation: It indicates that this is the controller class and its result writes into the response body and doesn't want to render view
  • @GetMapping annotation: It indicates a request handler method and it is a shorthand annotation for @RequestMapping(method = RequestMethod.GET) ...

Get Mastering Spring Boot 2.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.