
Yahoo Finance API – 完整指南
Spring Boot是Spring框架的一個擴展,它簡化了獨立、生產就緒的Spring應用程序的開發。通過提供預配置的設置,Spring Boot減少了樣板代碼,使開發人員能夠專注于業務邏輯。
在開始之前,請確保已安裝以下工具和環境:
com.example
rest-api
rest-api
com.example.restapi
創建一個簡單的數據模型類,用于表示API處理的數據。在本例中,我們將創建一個名為Book
的實體類。
package com.example.restapi.model;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String author;
private String isbn;
// Getters and Setters
}
創建一個存儲庫接口,繼承JpaRepository
,以便處理數據訪問操作。
package com.example.restapi.repository;
import com.example.restapi.model.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface BookRepository extends JpaRepository {
}
服務層封裝了業務邏輯,并與存儲庫交互。以下是服務類的示例:
package com.example.restapi.service;
import com.example.restapi.model.Book;
import com.example.restapi.repository.BookRepository;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookService {
private final BookRepository bookRepository;
public BookService(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
public List getAllBooks() {
return bookRepository.findAll();
}
public Book getBookById(Long id) {
return bookRepository.findById(id).orElse(null);
}
public Book createBook(Book book) {
return bookRepository.save(book);
}
public void deleteBook(Long id) {
bookRepository.deleteById(id);
}
}
創建一個REST控制器,用于處理HTTP請求并將其映射到服務方法。
package com.example.restapi.controller;
import com.example.restapi.model.Book;
import com.example.restapi.service.BookService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/books")
public class BookController {
private final BookService bookService;
public BookController(BookService bookService) {
this.bookService = bookService;
}
@GetMapping
public List getAllBooks() {
return bookService.getAllBooks();
}
@GetMapping("/{id}")
public Book getBookById(@PathVariable Long id) {
return bookService.getBookById(id);
}
@PostMapping
public Book createBook(@RequestBody Book book) {
return bookService.createBook(book);
}
@DeleteMapping("/{id}")
public void deleteBook(@PathVariable Long id) {
bookService.deleteBook(id);
}
}
在application.properties
文件中配置H2數據庫的連接信息:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
在IDE中運行主類,或者在終端中執行以下命令:
mvn spring-boot:run
GET http://localhost:8080/api/books
GET http://localhost:8080/api/books/1
POST http://localhost:8080/api/books
Content-Type: application/json
{
"title": "Spring Boot in Action",
"author": "Craig Walls",
"isbn": "9781617292545"
}
DELETE http://localhost:8080/api/books/1
在實際開發中,建議使用數據傳輸對象(DTO)來封裝API發送或接收的數據。DTO可以幫助:
以下是一個簡單的DTO示例:
package com.example.restapi.dto;
public class BookDTO {
private String title;
private String author;
// Getters and Setters
}
在服務層和控制器中引入DTO以優化數據傳輸。
通過本指南,您已經學會了如何使用Spring Boot構建一個功能完整的REST API。Spring Boot提供了強大的功能和靈活的配置,適用于各種規模的項目。您可以進一步探索安全性、驗證和錯誤處理等高級功能,以構建更加健壯的API。
原文鏈接: https://medium.com/@pratik.941/building-rest-api-using-spring-boot-a-comprehensive-guide-3e9b6d7a8951
Yahoo Finance API – 完整指南
WordPress REST API 內容注入漏洞分析
四款AI大模型API價格對比:DeepSeek R1、ChatGPT o3-mini、Grok3、通義千問 Max
四款AI大模型API基礎參數、核心性能的區別:DeepSeek R1、ChatGPT o3-mini、Grok3、通義千問 Max
2025年多模態大模型API基礎參數、核心性能:Deepseek、ChatGPT、文心一言
2025年最新推理大模型API價格對比:通義千問Max vs 豆包1.5 Pro vs 混元Lite
大模型新基座,基于FastAPI,利用Python開發MCP服務器
DeepSeek+ima:打造高效個人知識庫,提升學習與工作效率
快速接入騰訊地圖MCP Server