Guest User

Untitled

a guest
Jan 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. package com.example.demo;
  2.  
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.PostMapping;
  6. import org.springframework.web.bind.annotation.RequestBody;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. import org.springframework.web.bind.annotation.RestController;
  10.  
  11. @RestController
  12. @RequestMapping("books")
  13. public class BookController {
  14.  
  15. private final BookDao bookDao;
  16.  
  17. public BookController(BookDao bookDao) {
  18. this.bookDao = bookDao;
  19. }
  20.  
  21.  
  22. @GetMapping("/requestParam")
  23. public String requestParam(@RequestParam String valueA, @RequestParam(required = false) String valueB) {
  24. return "Value A is " + valueA + ", and valueB is " + valueB;
  25. }
  26.  
  27. @GetMapping("/pathParam/{valueA}/{valueB}")
  28. public String pathVariable(@PathVariable String valueA, @PathVariable String valueB) {
  29. return "Value A is " + valueA + ", and valueB is " + valueB;
  30. }
  31.  
  32. @GetMapping("/all")
  33. public long fetchBooks() {
  34. return bookDao.count();
  35. }
  36.  
  37. @PostMapping("/create")
  38. public BookDto createBook(@RequestBody BookDto bookDto) {
  39.  
  40. Book book = new Book();
  41. book.setTitle(bookDto.getTitle());
  42. bookDao.save(book);
  43.  
  44. return bookDto;
  45. }
  46.  
  47.  
  48. }
Add Comment
Please, Sign In to add comment