diff --git a/spring-5-mvc/pom.xml b/spring-5-mvc/pom.xml index 302080a8b4..7b641ffa79 100644 --- a/spring-5-mvc/pom.xml +++ b/spring-5-mvc/pom.xml @@ -3,9 +3,7 @@ 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"> 4.0.0 - com.baeldung spring-5-mvc - 0.0.1-SNAPSHOT spring-5-mvc spring 5 MVC sample project about new features jar @@ -87,6 +85,11 @@ ${jayway-rest-assured.version} test + + com.github.javafaker + javafaker + 0.18 + diff --git a/spring-5-mvc/src/main/java/com/baeldung/idc/Application.java b/spring-5-mvc/src/main/java/com/baeldung/idc/Application.java new file mode 100644 index 0000000000..ff86301f7d --- /dev/null +++ b/spring-5-mvc/src/main/java/com/baeldung/idc/Application.java @@ -0,0 +1,24 @@ +package com.baeldung.idc; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.PropertySource; + +@SpringBootApplication +@ComponentScan(basePackages = "com.baeldung.idc") +@PropertySource(value = { "classpath:/custom.properties" }, ignoreResourceNotFound = true) +public class Application { + + public static void main(String[] args) { + final SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class); + final Map map = new HashMap<>(); + map.put("spring.config.name", "custom.properties"); + builder.properties(map).run(args); + + } + +} diff --git a/spring-5-mvc/src/main/java/com/baeldung/idc/Book.java b/spring-5-mvc/src/main/java/com/baeldung/idc/Book.java new file mode 100644 index 0000000000..387f7049e1 --- /dev/null +++ b/spring-5-mvc/src/main/java/com/baeldung/idc/Book.java @@ -0,0 +1,55 @@ +package com.baeldung.idc; + +import org.springframework.stereotype.Component; + +@Component +public class Book { + + private final int id; + + private final String title; + + private final String author; + + private final String genre; + + public Book() { + this(-1, "", "", ""); + } + + public Book(int id, String title, String author, String genre) { + this.id = id; + this.title = title; + this.author = author; + this.genre = genre; + } + + /** + * @return the id + */ + public int getId() { + return id; + } + + /** + * @return the title + */ + public String getTitle() { + return title; + } + + /** + * @return the author + */ + public String getAuthor() { + return author; + } + + /** + * @return the genre + */ + public String getGenre() { + return genre; + } + +} diff --git a/spring-5-mvc/src/main/java/com/baeldung/idc/BookController.java b/spring-5-mvc/src/main/java/com/baeldung/idc/BookController.java new file mode 100644 index 0000000000..5b2349715d --- /dev/null +++ b/spring-5-mvc/src/main/java/com/baeldung/idc/BookController.java @@ -0,0 +1,34 @@ +package com.baeldung.idc; + +import java.util.List; +import java.util.Optional; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/book") +public class BookController implements BookOperations { + + private BookRepository repo; + + public BookController(BookRepository repo) { + this.repo = repo; + } + + @Override + public List getAll() { + return repo.getItems(); + } + + @Override + public Optional getById(int id) { + return repo.getById(id); + } + + @Override + public void save(Book book, int id) { + repo.save(id, book); + } + +} diff --git a/spring-5-mvc/src/main/java/com/baeldung/idc/BookOperations.java b/spring-5-mvc/src/main/java/com/baeldung/idc/BookOperations.java new file mode 100644 index 0000000000..70b44561af --- /dev/null +++ b/spring-5-mvc/src/main/java/com/baeldung/idc/BookOperations.java @@ -0,0 +1,23 @@ +package com.baeldung.idc; + +import java.util.List; +import java.util.Optional; + +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.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; + +@RequestMapping("/default") +public interface BookOperations { + + @GetMapping("/") + List getAll(); + + @GetMapping("/{id}") + Optional getById(@PathVariable int id); + + @PostMapping("/save/{id}") + public void save(@RequestBody Book book, @PathVariable int id); +} diff --git a/spring-5-mvc/src/main/java/com/baeldung/idc/BookRepository.java b/spring-5-mvc/src/main/java/com/baeldung/idc/BookRepository.java new file mode 100644 index 0000000000..0550b3b79a --- /dev/null +++ b/spring-5-mvc/src/main/java/com/baeldung/idc/BookRepository.java @@ -0,0 +1,61 @@ +package com.baeldung.idc; + +import java.util.List; +import java.util.Locale; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import javax.annotation.PostConstruct; + +import org.springframework.stereotype.Component; + +import com.github.javafaker.Faker; + +/** + * Repository for storing the books. + * + * It serves just for illustrative purposes and is completely in-memory. It uses Java Faker library in order to generate data. + * + * @author A.Shcherbakov + * + */ +@Component +public class BookRepository { + + private List items; + + @PostConstruct + public void init() { + Faker faker = new Faker(Locale.ENGLISH); + final com.github.javafaker.Book book = faker.book(); + this.items = IntStream.range(1, faker.random() + .nextInt(10, 20)) + .mapToObj(i -> new Book(i, book.title(), book.author(), book.genre())) + .collect(Collectors.toList()); + + } + + public int getCount() { + return items.size(); + } + + public List getItems() { + return items; + } + + public Optional getById(int id) { + return this.items.stream() + .filter(item -> id == item.getId()) + .findFirst(); + } + + public void save(int id, Book book) { + IntStream.range(0, items.size()) + .filter(i -> items.get(i) + .getId() == id) + .findFirst() + .ifPresent(i -> this.items.set(i, book)); + } + +} diff --git a/spring-5-mvc/src/main/resources/custom.properties b/spring-5-mvc/src/main/resources/custom.properties new file mode 100644 index 0000000000..78c6675b26 --- /dev/null +++ b/spring-5-mvc/src/main/resources/custom.properties @@ -0,0 +1,2 @@ +server.port=8080 +spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration \ No newline at end of file