Create an interface driven controller (BAEL-3095)

This commit is contained in:
Andrew Shcherbakov 2019-07-14 18:49:34 +02:00
parent ca53b93176
commit 7f747e44ec
7 changed files with 204 additions and 2 deletions

View File

@ -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>
@ -87,6 +85,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>

View File

@ -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<String, Object> map = new HashMap<>();
map.put("spring.config.name", "custom.properties");
builder.properties(map).run(args);
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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));
}
}

View File

@ -0,0 +1,2 @@
server.port=8080
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration