Skip to main content

Posts

Showing posts with the label spring boot

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...