Skip to content

Instantly share code, notes, and snippets.

@jirkapenzes
Created November 2, 2016 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jirkapenzes/39d62191a0d1580cd2cb3d2451ea0e15 to your computer and use it in GitHub Desktop.
Save jirkapenzes/39d62191a0d1580cd2cb3d2451ea0e15 to your computer and use it in GitHub Desktop.
Springboot demo
package books.entity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Author {
private String firstName;
private String lastName;
public Author() {
}
public Author(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Author{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
package books.entity;
public class Book {
private String name;
private String isbn;
private int publishYear;
private Author author;
public Book() {
}
public Book(String name, String isbn, int publishYear, Author author) {
this.name = name;
this.isbn = isbn;
this.publishYear = publishYear;
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public int getPublishYear() {
return publishYear;
}
public void setPublishYear(int publishYear) {
this.publishYear = publishYear;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", isbn='" + isbn + '\'' +
", publishYear=" + publishYear +
", author=" + author +
'}';
}
}
package books;
import books.entity.Author;
import books.entity.Book;
import java.util.ArrayList;
import java.util.List;
public class BookRepository {
private List<Book> books;
public BookRepository() {
books = new ArrayList<Book>();
initializeSampleData();
}
private void initializeSampleData() {
books.add(new Book("Kmotr", "80-242-1102-5", 1969, new Author("Mario", "Puzo")));
books.add(new Book("Omerta", "80-242-1642-6", 2000, new Author("Mario", "Puzo")));
books.add(new Book("Inferno", "978-80-257-0934-4", 2013, new Author("Dan", "Brown")));
books.add(new Book("Stoletý stařík, který vylezl z okna a zmizel", "978-80-87697-00-9", 2009, new Author("Jonas", "Jonasson")));
books.add(new Book("Digitální pevnost", "80-86518-95-7", 1998, new Author("Dan", "Brown")));
books.add(new Book("Modrá sféra", "80-7303-047-0", 2001, new Author("Jeffery", "Deaver")));
}
public List<Book> findAll() {
return books;
}
public Book findByIsbn(String isbn) {
return books.stream()
.filter(book -> book.getIsbn().equals(isbn))
.findFirst().orElse(null);
}
public Book add(Book book) {
boolean exist = findByIsbn(book.getIsbn()) != null;
if (exist)
throw new RuntimeException("Books exist - ISBN: " + book.getIsbn());
books.add(book);
return book;
}
public Book remove(String isbn) {
Book book = findByIsbn(isbn);
if (book != null) {
books.remove(book);
return book;
}
throw new RuntimeException("Books doesn't exist - ISBN: " + book.getIsbn());
}
public Book update(String isbn, Book changes) {
Book currentBook = findByIsbn(isbn);
if (currentBook == null) {
throw new RuntimeException("Books doesn't exist - ISBN: " + isbn);
}
if (!isbn.equals(changes.getIsbn()) && findByIsbn(changes.getIsbn()) != null) {
throw new RuntimeException("Duplicate book - ISBN: " + isbn);
}
currentBook.setIsbn(changes.getIsbn());
currentBook.setName(changes.getName());
currentBook.setPublishYear(changes.getPublishYear());
currentBook.setAuthor(changes.getAuthor());
return currentBook;
}
}
package books;
import books.entity.Book;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
@Controller
@EnableAutoConfiguration
public class BooksController {
private final BookRepository bookRepository;
public BooksController() {
this.bookRepository = new BookRepository();
}
@ResponseBody
@RequestMapping(value = "/books", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Collection<Book> getAll() {
return bookRepository.findAll();
}
@ResponseBody
@RequestMapping(value = "/books/{isbn}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Book findByIsbn(@PathVariable String isbn) {
return bookRepository.findByIsbn(isbn);
}
@ResponseBody
@RequestMapping(value = "/books/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public Book add(@RequestBody Book book) {
return bookRepository.add(book);
}
@ResponseBody
@RequestMapping(value = "/books/{isbn}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
public Book update(@PathVariable String isbn, @RequestBody Book book) {
return bookRepository.update(isbn, book);
}
@ResponseBody
@RequestMapping(value = "/books/{isbn}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
public Book remove(@PathVariable String isbn) {
return bookRepository.remove(isbn);
}
public static void main(String[] args) {
SpringApplication.run(BooksController.class, args);
}
}
package exchangesrates;
import com.amazonaws.util.json.JSONException;
import com.amazonaws.util.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RestClientDemo {
// http://fixer.io/
public static void main(String[] args) throws IOException, JSONException {
// console();
entity();
}
private static void entity() throws IOException, JSONException {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://api.fixer.io/latest");
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
JSONObject json = new JSONObject(result.toString());
System.out.println(json);
}
public static void console() throws IOException {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://api.fixer.io/latest");
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
}
}
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment