6.7 實作一個 Controller Class
我們實作一個 Controller Class,用處理來自前端裝置的 http request 及做正確的 http response。
程式源碼如下 :
package edume.microservice.controller;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import edume.microservice.model.Book;
import edume.microservice.service.IBookService;
@RestController
@PostMapping("/api/v1/")
public class BookController
{
private Log logger = LogFactory.getLog(BookController.class);
private IBookService service = null;
@Autowired
public BookController(IBookService service)
{
this.service = service;
}
@PostMapping("/book")
public Map<String, Object> create(@RequestBody Map<String, Object> bookMap)
{
String name = bookMap.get("name").toString();
String isbn = bookMap.get("isbn").toString();
String author = bookMap.get("author").toString();
int page = Integer.parseInt(bookMap.get("page").toString());
Book book = new Book(name, isbn, author, page);
this.service.createBook(book);
Map<String, Object> response = new LinkedHashMap<String, Object>();
response.put("message", "New book created successfully !");
response.put("book", book);
return response;
}
@GetMapping( "/account/{bookId}")
public Book getBookDetails(@PathVariable("bookId") String bookId)
{
return this.service.getBook(bookId);
}
@GetMapping("/books")
public Map<String, Object> getAllBooks()
{
List<Book> books = this.service.getAllBooks();
Map<String, Object> response = new LinkedHashMap<String, Object>();
response.put("totalBooks", books.size());
response.put("books", books);
return response;
}
@PutMapping("/account/{bookId}")
public Map<String, Object> editBook(@PathVariable("bookId") String bookId, @RequestBody Map<String, Object> bookMap)
{
String name = bookMap.get("name").toString();
String isbn = bookMap.get("isbn").toString();
String author = bookMap.get("author").toString();
int page = Integer.parseInt(bookMap.get("page").toString());
logger.info(
"update book >>> Id : " + bookId + ", name :" + name + ", isbn :" + isbn + ", author :" + author + ", page :" + page);
Book book = new Book(name, isbn, author, page);
book.setId(bookId);
this.service.updateBook(book);
Map<String, Object> response = new LinkedHashMap<String, Object>();
response.put("message", "Book Updated successfully");
response.put("book", book);
return response;
}
@DeleteMapping("/account/{bookId}")
public Map<String, String> deleteBook(@PathVariable("bookId") String bookId)
{
this.service.deleteBook(bookId);
Map<String, String> response = new HashMap<String, String>();
response.put("message", "Book deleted successfully");
return response;
}
}