Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jwierzbo committed Feb 5, 2018
0 parents commit a009984
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
target
*.iml
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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```
54 changes: 54 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>

<groupId>net.jwierzbo.rest</groupId>
<artifactId>REST-movie-service</artifactId>
<version>0.1</version>
<name>Movie app - Spring Boot REST with CRUD example</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
12 changes: 12 additions & 0 deletions src/main/java/net/jwierzbo/rest/Application.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
73 changes: 73 additions & 0 deletions src/main/java/net/jwierzbo/rest/dao/MovieDAO.java
Original file line number Diff line number Diff line change
@@ -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<Movie> 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;
}

}
58 changes: 58 additions & 0 deletions src/main/java/net/jwierzbo/rest/model/Movie.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit a009984

Please sign in to comment.