Spring Boot Controller vs RestController

Spring RestController vs Controller

Spring 4 introduced a new annotation called RestController. It makes writing REST controllers a little easy as we don't have to repeat the @ResponseBody annotations in each and every controller method. In this short article, we will examine how to write the REST API controller using both the annotations. While writing the REST controller it is recommended to use @RestController rather then @Controller.

@Controller Example

Controller annotation is a classic annotation which marks a Class as a controller and helps Spring to autodetect these Classes though Class scanning. While developing REST API in Spring Boot, we can mark a Class as Controller. We then need to use the @ResponseBody annotation to be used so that whatever the method returns can be serialized.
In this article, we will go through two examples a simple Controller which returns a list of categories and tags as JSON response respectively using both these annotations.

Let's see an example with Controller annotation.
CategoryController controller contains a method "getBlogCats" that returns a list of categories, it is mapped to the request path "/blogapi/categories". Here we are using @Controller annotation to mark this class as Controller. We need to use @ResponseBody annotation in the method getBlogCats so that List can be serialized and sent back to the client as Web response.
CategoryController Class
@Controller
@RequestMapping("/blogapi")
public class CategoryController {

  private static final Logger log = LoggerFactory.getLogger(CategoryController.class);

  @Autowired
  BlogService blogService;

  @RequestMapping(value = {"/categories"}, method = RequestMethod.GET,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody ResponseEntity<List<String>> getBlogCats() {
    ResponseEntity<List<String>> apiResponse;
    try {
      List<String> categories = blogService.getBlogCategories();
      apiResponse = new ResponseEntity<List<String>>(categories, HttpStatus.OK);
    } catch (AppException e) {
      apiResponse = new ResponseEntity<List<String>>(HttpStatus.INTERNAL_SERVER_ERROR);
      log.error(e.getMessage());
    }
    return apiResponse;
  }
}
Next we will see a REST controller with @RestController. This Controller creates a GET REST endpoint that returns a list of Tags. It can be accessed as GET /blogapi/tags. The method getBlogTags() returns ResponseEntity>. Since we are marking the Class with @RestController, Spring will serialize the return type to HTTP response body and return to the client. Notice that we don't need to use @ResponseBody annotation.

@RestController Example

TagAPIController.Java
@RestController
@RequestMapping("/blogapi")
public class TagAPIController {
  private static final Logger log = LoggerFactory.getLogger(TagAPIController.class);
  @Autowired
  BlogService blogService;

  @RequestMapping(value = {"/tags"}, method = RequestMethod.GET,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity getBlogTags() {
    ResponseEntity<List<String>> apiResponse;
    try {
      List<String> tags = blogService.getBlogTags();
      apiResponse = new ResponseEntity<List<String>>(tags, HttpStatus.OK);
    } catch (AppException e) {
      apiResponse = new ResponseEntity<List<String>>(HttpStatus.INTERNAL_SERVER_ERROR);
      log.error(e.getMessage());
    }
    return apiResponse;
  }
}
In this example since we marked the controller with @RestController , we don't need to mark getBlogTags with @ResponseBody explicitly.
Summary
  • @RestController is available from Spring 4.0 onwards.
  • @RestController is a composed annotation containing both @Controller and @ResponseBody annotations.
  • While writing REST controller it is better to use @RestController.
  • In this article, we have seen two different Controllers one using @Controller and another using @RestController.

References

No comments :

Post a Comment

Please leave your message queries or suggetions.