这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions spring-boot-rest-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.hateoasvsswagger.SpringBootRestApplication</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
Expand All @@ -55,10 +58,20 @@
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>com.ly.smart-doc</groupId>
<artifactId>smart-doc-maven-plugin</artifactId>
<version>${smart-doc.version}</version>
<configuration>
<configFile>./src/main/resources/smart-doc.json</configFile>
<projectName>Book API</projectName>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<springdoc-openapi.version>2.5.0</springdoc-openapi.version>
<smart-doc.version>3.1.1</smart-doc.version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung;
package com.baeldung.hateoasvsswagger;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
63 changes: 63 additions & 0 deletions spring-boot-rest-2/src/main/java/com/baeldung/smartdoc/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.baeldung.smartdoc;

public class Book {

/**
* Book ID
*/
private Long id;
/**
* Author
*/
private String author;
/**
* Book Title
*/
private String title;
/**
* Book Price
*/
private Double price;

public Book() {
}

public Book(Long id, String author, String title, Double price) {
this.id = id;
this.author = author;
this.title = title;
this.price = price;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Double getPrice() {
return price;
}

public void setPrice(Double price) {
this.price = price;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baeldung.smartdoc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BookApplication {

public static void main(String[] args) {
SpringApplication.run(BookApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.baeldung.smartdoc;

import java.util.List;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

/**
* The type Book controller.
*
* @author Baeldung.
*/

@RestController
@RequestMapping("/api/v1")
public class BookController {

@Autowired
private BookRepository bookRepository;

/**
* Create book.
*
* @param book the book
* @return the book
*/
@PostMapping("/books")
public ResponseEntity<Book> createBook(@RequestBody Book book) {
bookRepository.add(book);

return ResponseEntity.ok(book);
}

/**
* Get all books.
*
* @return the list
*/
@GetMapping("/books")
public ResponseEntity<List<Book>> getAllBooks() {
return ResponseEntity.ok(bookRepository.getBooks());
}

/**
* Gets book by id.
*
* @param bookId the book id|1
* @return the book by id
*/
@GetMapping("/book/{id}")
public ResponseEntity<Book> getBookById(@PathVariable(value = "id") Long bookId) {
Book book = bookRepository.findById(bookId)
.orElseThrow(() -> new ResponseStatusException(HttpStatusCode.valueOf(404)));
return ResponseEntity.ok(book);
}

/**
* Update book response entity.
*
* @param bookId the book id|1
* @param bookDetails the book details
* @return the response entity
*/
@PutMapping("/books/{id}")
public ResponseEntity<Book> updateBook(@PathVariable(value = "id") Long bookId, @Valid @RequestBody Book bookDetails) {
Book book = bookRepository.findById(bookId)
.orElseThrow(() -> new ResponseStatusException(HttpStatusCode.valueOf(404)));
book.setAuthor(bookDetails.getAuthor());
book.setPrice(bookDetails.getPrice());
book.setTitle(bookDetails.getTitle());

bookRepository.add(book);
return ResponseEntity.ok(book);
}

/**
* Delete book.
*
* @param bookId the book id|1
* @return the true
*/
@DeleteMapping("/book/{id}")
public ResponseEntity<Boolean> deleteBook(@PathVariable(value = "id") Long bookId) {
Book book = bookRepository.findById(bookId)
.orElseThrow(() -> new ResponseStatusException(HttpStatusCode.valueOf(404)));
return ResponseEntity.ok(bookRepository.delete(book));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.baeldung.smartdoc;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.stereotype.Repository;

@Repository
public class BookRepository {

private static final Map<Long, Book> books = new ConcurrentHashMap<>();

static {
Book book = new Book(1L, "George Martin", "A Song of Ice and Fire", 10000.00);
books.put(1L, book);
}

public Optional<Book> findById(long id) {
return Optional.ofNullable(books.get(id));
}

public void add(Book book) {
books.put(book.getId(), book);
}

public List<Book> getBooks() {
return new ArrayList<>(books.values());
}

public boolean delete(Book book) {
return books.remove(book.getId(), book);
}

}
Loading