Skip to main content

Posts

Showing posts with the label rest

Exception Handling with Spring Boot Application

Exception and error handling is one of the most crucial when it comes to web service. It is also the most important as the correct respond should be given to the service consumer. This is not an easy task. You may have to code a wrapper class for error handling. But with spring boot this has become very easy. This blog post is about how a thrown exception can be handled as the response. METHOD 1: package com . example . project . exceptions ; import org.springframework.http.HttpStatus ; import org.springframework.web.bind.annotation.ResponseStatus ; @ResponseStatus ( value = HttpStatus . NOT_FOUND , reason = "No such Movie" ) public class MovieNotFoundException extends Exception { } The exception class must be annotated with @ResponseStatus. This defines the response to be returned when the exception is thrown. So if the controller is as follows, package com . example . project . controllers ; import com.example.package.models.Movie ; i...

A noob Introduction to creating a REST API using Spring Boot and MongoDB

Intro This post is a beginner's guide to getting started with creating a simple REST API within seconds using Spring Boot and MongoDB. Contains all basic CRUD operations. Few confusions I faced as a beginner was when packaging was done and Spring Boot could locate several components. So this blog post will follow the standard packaging alongside the implementation. For further understanding of the annotations to be used, [1] and [2] can be referenced Prerequisites  Maven MongoDB setup Code Step 1 : Initiate maven directory structure |__src          |__main                      |__java                                 |__com.example.project                      |__resources         |__test Initiate the pom.xml as...

HATEOAS for REST APIs

What is HATEOAS? HATEOAS stands for Hypermedia as the Engine of Application State which is a constraint of the REST application architecture. REST APIs has no service definition and no formal documentation. The best REST APIs don't need any documentation. Just like websites have navigation from one page to another, REST APIs are able to do the same using HATEOAS. In HATEOAS the response will carry links that provide links as to where to find the related resources. This will let clients know all the things they can do with the received response and make the response navigable. Eg: An user object may contain the URI to itself.  { " name " : " John Doe " , " links " : [ { " rel " : " self " , " href " : " http://localhost:8080/users/1 " } ] } 'rel' attribute here defines the relationship. If we take a status object of a particular social network site, it could...