Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.demo;
- 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;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- @RequestMapping("books")
- public class BookController {
- private final BookDao bookDao;
- public BookController(BookDao bookDao) {
- this.bookDao = bookDao;
- }
- @GetMapping("/requestParam")
- public String requestParam(@RequestParam String valueA, @RequestParam(required = false) String valueB) {
- return "Value A is " + valueA + ", and valueB is " + valueB;
- }
- @GetMapping("/pathParam/{valueA}/{valueB}")
- public String pathVariable(@PathVariable String valueA, @PathVariable String valueB) {
- return "Value A is " + valueA + ", and valueB is " + valueB;
- }
- @GetMapping("/all")
- public long fetchBooks() {
- return bookDao.count();
- }
- @PostMapping("/create")
- public BookDto createBook(@RequestBody BookDto bookDto) {
- Book book = new Book();
- book.setTitle(bookDto.getTitle());
- bookDao.save(book);
- return bookDto;
- }
- }
Add Comment
Please, Sign In to add comment