-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBookController.java
More file actions
40 lines (32 loc) · 1.04 KB
/
BookController.java
File metadata and controls
40 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/books")
public class BookController {
private BookRepository repository;
@Autowired
public BookController(BookRepository repository) {
this.repository = repository;
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<Book> get(@PathVariable("id") Long id) {
Book book = repository.findOne(id);
if (null == book) {
return new ResponseEntity<Book>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<Book>(book, HttpStatus.OK);
}
@RequestMapping(value = "/new", method = RequestMethod.POST)
public ResponseEntity<Book> update(@RequestBody Book book) {
repository.save(book);
return get(book.getId());
}
@RequestMapping
public List<Book> all() {
return repository.findAll();
}
}