Skip to content

File tree

17 files changed

+2142
-1
lines changed

17 files changed

+2142
-1
lines changed

spring-boot-rest-2/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
<plugin>
4545
<groupId>org.springframework.boot</groupId>
4646
<artifactId>spring-boot-maven-plugin</artifactId>
47+
<configuration>
48+
<mainClass>com.baeldung.hateoasvsswagger.SpringBootRestApplication</mainClass>
49+
</configuration>
4750
</plugin>
4851
<plugin>
4952
<artifactId>maven-compiler-plugin</artifactId>
@@ -55,10 +58,20 @@
5558
<target>17</target>
5659
</configuration>
5760
</plugin>
61+
<plugin>
62+
<groupId>com.ly.smart-doc</groupId>
63+
<artifactId>smart-doc-maven-plugin</artifactId>
64+
<version>${smart-doc.version}</version>
65+
<configuration>
66+
<configFile>./src/main/resources/smart-doc.json</configFile>
67+
<projectName>Book API</projectName>
68+
</configuration>
69+
</plugin>
5870
</plugins>
5971
</build>
6072

6173
<properties>
6274
<springdoc-openapi.version>2.5.0</springdoc-openapi.version>
75+
<smart-doc.version>3.1.1</smart-doc.version>
6376
</properties>
6477
</project>

spring-boot-rest-2/src/main/java/com/baeldung/SpringBootRestApplication.java renamed to spring-boot-rest-2/src/main/java/com/baeldung/hateoasvsswagger/SpringBootRestApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung;
1+
package com.baeldung.hateoasvsswagger;
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.smartdoc;
2+
3+
public class Book {
4+
5+
/**
6+
* Book ID
7+
*/
8+
private Long id;
9+
/**
10+
* Author
11+
*/
12+
private String author;
13+
/**
14+
* Book Title
15+
*/
16+
private String title;
17+
/**
18+
* Book Price
19+
*/
20+
private Double price;
21+
22+
public Book() {
23+
}
24+
25+
public Book(Long id, String author, String title, Double price) {
26+
this.id = id;
27+
this.author = author;
28+
this.title = title;
29+
this.price = price;
30+
}
31+
32+
public Long getId() {
33+
return id;
34+
}
35+
36+
public void setId(Long id) {
37+
this.id = id;
38+
}
39+
40+
public String getAuthor() {
41+
return author;
42+
}
43+
44+
public void setAuthor(String author) {
45+
this.author = author;
46+
}
47+
48+
public String getTitle() {
49+
return title;
50+
}
51+
52+
public void setTitle(String title) {
53+
this.title = title;
54+
}
55+
56+
public Double getPrice() {
57+
return price;
58+
}
59+
60+
public void setPrice(Double price) {
61+
this.price = price;
62+
}
63+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.smartdoc;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class BookApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(BookApplication.class, args);
11+
}
12+
13+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.baeldung.smartdoc;
2+
3+
import java.util.List;
4+
5+
import jakarta.annotation.Resource;
6+
import jakarta.validation.Valid;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.http.HttpStatusCode;
10+
import org.springframework.http.ResponseEntity;
11+
import org.springframework.web.bind.annotation.DeleteMapping;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.PathVariable;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.PutMapping;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
import org.springframework.web.bind.annotation.RequestMapping;
18+
import org.springframework.web.bind.annotation.RestController;
19+
import org.springframework.web.server.ResponseStatusException;
20+
21+
/**
22+
* The type Book controller.
23+
*
24+
* @author Baeldung.
25+
*/
26+
27+
@RestController
28+
@RequestMapping("/api/v1")
29+
public class BookController {
30+
31+
@Autowired
32+
private BookRepository bookRepository;
33+
34+
/**
35+
* Create book.
36+
*
37+
* @param book the book
38+
* @return the book
39+
*/
40+
@PostMapping("/books")
41+
public ResponseEntity<Book> createBook(@RequestBody Book book) {
42+
bookRepository.add(book);
43+
44+
return ResponseEntity.ok(book);
45+
}
46+
47+
/**
48+
* Get all books.
49+
*
50+
* @return the list
51+
*/
52+
@GetMapping("/books")
53+
public ResponseEntity<List<Book>> getAllBooks() {
54+
return ResponseEntity.ok(bookRepository.getBooks());
55+
}
56+
57+
/**
58+
* Gets book by id.
59+
*
60+
* @param bookId the book id|1
61+
* @return the book by id
62+
*/
63+
@GetMapping("/book/{id}")
64+
public ResponseEntity<Book> getBookById(@PathVariable(value = "id") Long bookId) {
65+
Book book = bookRepository.findById(bookId)
66+
.orElseThrow(() -> new ResponseStatusException(HttpStatusCode.valueOf(404)));
67+
return ResponseEntity.ok(book);
68+
}
69+
70+
/**
71+
* Update book response entity.
72+
*
73+
* @param bookId the book id|1
74+
* @param bookDetails the book details
75+
* @return the response entity
76+
*/
77+
@PutMapping("/books/{id}")
78+
public ResponseEntity<Book> updateBook(@PathVariable(value = "id") Long bookId, @Valid @RequestBody Book bookDetails) {
79+
Book book = bookRepository.findById(bookId)
80+
.orElseThrow(() -> new ResponseStatusException(HttpStatusCode.valueOf(404)));
81+
book.setAuthor(bookDetails.getAuthor());
82+
book.setPrice(bookDetails.getPrice());
83+
book.setTitle(bookDetails.getTitle());
84+
85+
bookRepository.add(book);
86+
return ResponseEntity.ok(book);
87+
}
88+
89+
/**
90+
* Delete book.
91+
*
92+
* @param bookId the book id|1
93+
* @return the true
94+
*/
95+
@DeleteMapping("/book/{id}")
96+
public ResponseEntity<Boolean> deleteBook(@PathVariable(value = "id") Long bookId) {
97+
Book book = bookRepository.findById(bookId)
98+
.orElseThrow(() -> new ResponseStatusException(HttpStatusCode.valueOf(404)));
99+
return ResponseEntity.ok(bookRepository.delete(book));
100+
}
101+
102+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.smartdoc;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.Optional;
7+
import java.util.concurrent.ConcurrentHashMap;
8+
9+
import org.springframework.stereotype.Repository;
10+
11+
@Repository
12+
public class BookRepository {
13+
14+
private static final Map<Long, Book> books = new ConcurrentHashMap<>();
15+
16+
static {
17+
Book book = new Book(1L, "George Martin", "A Song of Ice and Fire", 10000.00);
18+
books.put(1L, book);
19+
}
20+
21+
public Optional<Book> findById(long id) {
22+
return Optional.ofNullable(books.get(id));
23+
}
24+
25+
public void add(Book book) {
26+
books.put(book.getId(), book);
27+
}
28+
29+
public List<Book> getBooks() {
30+
return new ArrayList<>(books.values());
31+
}
32+
33+
public boolean delete(Book book) {
34+
return books.remove(book.getId(), book);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)