From a00998437e0da2e90d943a7d14fb2319f42cd7ee Mon Sep 17 00:00:00 2001 From: jwierzbo Date: Mon, 5 Feb 2018 23:57:37 +0100 Subject: [PATCH] Initial commit --- .gitignore | 3 + README.md | 14 ++++ pom.xml | 54 ++++++++++++++ .../java/net/jwierzbo/rest/Application.java | 12 +++ .../rest/controller/MovieRestController.java | 67 +++++++++++++++++ .../java/net/jwierzbo/rest/dao/MovieDAO.java | 73 +++++++++++++++++++ .../java/net/jwierzbo/rest/model/Movie.java | 58 +++++++++++++++ 7 files changed, 281 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 pom.xml create mode 100644 src/main/java/net/jwierzbo/rest/Application.java create mode 100644 src/main/java/net/jwierzbo/rest/controller/MovieRestController.java create mode 100644 src/main/java/net/jwierzbo/rest/dao/MovieDAO.java create mode 100644 src/main/java/net/jwierzbo/rest/model/Movie.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..18f0443 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +target +*.iml \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..340b051 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# Spring Boot REST Controller Example +REST CRUD Example for Spring Boot + +1. To Run this project locally: + ```shell + $ mvn spring-boot:run + ``` +1. To build and run from executable jar: + ```shell + $ mvn package + $ java -jar target/REST-movie-service-0.1.jar + ``` +1. To access Movies app, open: + ```http://localhost:8080/movies``` diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..79cd452 --- /dev/null +++ b/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 1.5.10.RELEASE + + + net.jwierzbo.rest + REST-movie-service + 0.1 + Movie app - Spring Boot REST with CRUD example + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + spring-releases + https://repo.spring.io/libs-release + + + + + spring-releases + https://repo.spring.io/libs-release + + + diff --git a/src/main/java/net/jwierzbo/rest/Application.java b/src/main/java/net/jwierzbo/rest/Application.java new file mode 100644 index 0000000..dc0dd52 --- /dev/null +++ b/src/main/java/net/jwierzbo/rest/Application.java @@ -0,0 +1,12 @@ +package net.jwierzbo.rest; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/src/main/java/net/jwierzbo/rest/controller/MovieRestController.java b/src/main/java/net/jwierzbo/rest/controller/MovieRestController.java new file mode 100644 index 0000000..b09d869 --- /dev/null +++ b/src/main/java/net/jwierzbo/rest/controller/MovieRestController.java @@ -0,0 +1,67 @@ +package net.jwierzbo.rest.controller; + +import java.util.List; + +import net.jwierzbo.rest.dao.MovieDAO; +import net.jwierzbo.rest.model.Movie; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class MovieRestController { + + @Autowired + private MovieDAO movieDAO; + + @GetMapping("/movies") + public List getMovies() { + return movieDAO.list(); + } + + @GetMapping("/movies/{id}") + public ResponseEntity getMovie(@PathVariable("id") Long id) { + Movie movie = movieDAO.get(id); + if (movie == null) { + return new ResponseEntity("No Movie found for ID " + id, HttpStatus.NOT_FOUND); + } + + return new ResponseEntity(movie, HttpStatus.OK); + } + + @PostMapping(value = "/movies") + public ResponseEntity createCustomer(@RequestBody Movie movie) { + movieDAO.create(movie); + + return new ResponseEntity(movie, HttpStatus.OK); + } + + @DeleteMapping("/movies/{id}") + public ResponseEntity deleteMovie(@PathVariable Long id) { + if (null == movieDAO.delete(id)) { + return new ResponseEntity("No Movie found for ID " + id, HttpStatus.NOT_FOUND); + } + + return new ResponseEntity(id, HttpStatus.OK); + + } + + @PutMapping("/movies/{id}") + public ResponseEntity updateMovie(@PathVariable Long id, @RequestBody Movie movie) { + movie = movieDAO.update(id, movie); + + if (null == movie) { + return new ResponseEntity("No Movie found for ID " + id, HttpStatus.NOT_FOUND); + } + + return new ResponseEntity(movie, HttpStatus.OK); + } + +} \ No newline at end of file diff --git a/src/main/java/net/jwierzbo/rest/dao/MovieDAO.java b/src/main/java/net/jwierzbo/rest/dao/MovieDAO.java new file mode 100644 index 0000000..1101697 --- /dev/null +++ b/src/main/java/net/jwierzbo/rest/dao/MovieDAO.java @@ -0,0 +1,73 @@ +package net.jwierzbo.rest.dao; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; + +import net.jwierzbo.rest.model.Movie; +import org.springframework.stereotype.Component; + +@Component +public class MovieDAO { + + private final AtomicLong counter = new AtomicLong(); + + private static List movies; + { + movies = new ArrayList(); + movies.add(new Movie(counter.incrementAndGet(), "Django", "Quentin Tarantino", + LocalDate.of(2012,12, 11))); + movies.add(new Movie(counter.incrementAndGet(), "Gran Torino", "Clint Eastwood", + LocalDate.of(2008,12, 9))); + movies.add(new Movie(counter.incrementAndGet(), "Taxi Driver", "Martin Scorsese", + LocalDate.of(1976,02, 8))); + } + + public List list() { + return movies; + } + + public Movie get(Long id) { + + for (Movie c : movies) { + if (c.getId().equals(id)) { + return c; + } + } + return null; + } + + public Movie create(Movie movie) { + movie.setId(counter.incrementAndGet()); + movies.add(movie); + return movie; + } + + public Long delete(Long id) { + + for (Movie c : movies) { + if (c.getId().equals(id)) { + movies.remove(c); + return id; + } + } + + return null; + } + + public Movie update(Long id, Movie movie) { + + for (Movie c : movies) { + if (c.getId().equals(id)) { + movie.setId(c.getId()); + movies.remove(c); + movies.add(movie); + return movie; + } + } + + return null; + } + +} \ No newline at end of file diff --git a/src/main/java/net/jwierzbo/rest/model/Movie.java b/src/main/java/net/jwierzbo/rest/model/Movie.java new file mode 100644 index 0000000..9beb687 --- /dev/null +++ b/src/main/java/net/jwierzbo/rest/model/Movie.java @@ -0,0 +1,58 @@ +package net.jwierzbo.rest.model; + +import com.fasterxml.jackson.annotation.JsonFormat; + +import java.time.LocalDate; + +public class Movie { + + private Long id; + private String title; + private String director; + + @JsonFormat(pattern = "yyyy-MM-dd") + private LocalDate releaseDate; + + public Movie(long id, String title, String director, LocalDate releaseDate) { + this.id = id; + this.title = title; + this.director = director; + this.releaseDate = releaseDate; + } + + public Movie() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDirector() { + return director; + } + + public void setDirector(String director) { + this.director = director; + } + + public LocalDate getReleaseDate() { + return releaseDate; + } + + public void setReleaseDate(LocalDate releaseDate) { + this.releaseDate = releaseDate; + } + +} \ No newline at end of file