Merge pull request #7310 from veontomo/BAEL-3095
Create an interface driven controller (BAEL-3095)
This commit is contained in:
commit
88de49bc69
|
@ -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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-5-mvc</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-5-mvc</name>
|
||||
<description>spring 5 MVC sample project about new features</description>
|
||||
<packaging>jar</packaging>
|
||||
|
@ -83,6 +81,11 @@
|
|||
<version>${jayway-rest-assured.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.javafaker</groupId>
|
||||
<artifactId>javafaker</artifactId>
|
||||
<version>0.18</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.idc;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Book> getAll() {
|
||||
return repo.getItems();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Book> getById(int id) {
|
||||
return repo.getById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Book book, int id) {
|
||||
repo.save(id, book);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Book> getAll();
|
||||
|
||||
@GetMapping("/{id}")
|
||||
Optional<Book> getById(@PathVariable int id);
|
||||
|
||||
@PostMapping("/save/{id}")
|
||||
public void save(@RequestBody Book book, @PathVariable int id);
|
||||
}
|
|
@ -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<Book> 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<Book> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public Optional<Book> 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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue