A Basic REST API in Java Springboot

For Developing a basic REST API we need to :

  • Create a maven project by going to https://start.spring.io/ and giving group , artifact , name , description and package name. Java Version is 8 (Used in the company).
  • The only dependency for creating REST is Spring Web.
  • Download the project and open it in IntelliJ by importing the pom.xml.
  • The basic structure will be like follows:
//This is the basic model class which has all the getter and setter for the API
//Seperating the components

public class Movie {

    private String MovieId;

    public String getMovieId() {
        return MovieId;
    }

    public Movie() {
    }

    public void setMovieId(String movieId) {
        MovieId = movieId;
    }

    public Movie(String movieId, String name) {
        MovieId = movieId;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;
}
//This is the class which will have API for get movies info .Default is GET so if anyone wants to do POST,
//DELETE we need to given it in request mapping.
//@PathVariable is the vaibale passed in the url of the API 

@RestController
@RequestMapping("/movies")
public class MovieResource {

    @RequestMapping("/{movieId}")
    public Movie getMovieInfo(@PathVariable("movieId") String movieId)
    {
        return new Movie(movieId,"TestName");
    }
}
  • If we run this API then we will get following output . The Path will be http://localhost:8081/movies/1234
    where 1234 is the movieId.

Link for the project: https://github.com/hi2saif/SpringbootSampleProject/tree/master/movie-info-service