---
title: 'Springboot 查詢的 restfulapi(不使用資料庫)'
disqus: hackmd
---
###### tags: `SpringBoot`
Springboot 查詢的 restfulapi(不使用資料庫)
===
[TOC]
## 筆記目的
完成一個專案,讓你輸入某個網址,就得到對應的json格式的資料
具體請參考:
* [新手工程師](https://chikuwa-tech-study.blogspot.com/2021/05/spring-boot-three-tier-architecture.html)
## 注意事項
由於是簡單的專案,所以尚未去串接資料庫,所以一旦關掉程式,對應的資料也會不見請注意
由於專案簡單,所以先忽略service的部分
## 程式碼部分
### 專案架構

### pom.xml
```xml=
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloworld</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
```
### Product (Bean)
```java=
package com.example.demo.entity;
public class Product {
private String id;
private String name;
private int price;
public Product() {
}
public Product(String id, String name, int price) {
this.id = id;
this.name = name;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
```
### DemoProductDAO
```java=
package com.example.demo.dao;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Repository;
import com.example.demo.entity.*;
@Repository
public class DemoProductDAO {
private final List<Product> productDB = new ArrayList<>();
@PostConstruct
private void initDB() {
productDB.add(new Product("B0001", "Android Development (Java)", 380));
productDB.add(new Product("B0002", "Android Development (Kotlin)", 420));
productDB.add(new Product("B0003", "Data Structure (Java)", 250));
productDB.add(new Product("B0004", "Finance Management", 450));
productDB.add(new Product("B0005", "Human Resource Management", 330));
}
public Product insert(Product product) {
productDB.add(product);
return product;
}
public List<Product> showallProduct( ) {
return productDB;
}
}
```
### DemoController
```java=
package com.example.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.dao.DemoProductDAO;
import com.example.demo.entity.Product;
@RestController
public class DemoController {
@Autowired
private DemoProductDAO demoProductDAO;
@RequestMapping(value = "/demo", method = RequestMethod.GET)
public String demo(){
return "Hello World!";
}
@RequestMapping(value = "/showallProduct", method = RequestMethod.GET)
public List<Product> showallProduct(){
return demoProductDAO.showallProduct();
}
}
```
### 專案解析
Product 這個class基本上就只是定義身為一個產品該要有的三個要素各為何,基本上只是設計圖
DemoProductDAO 幫助了我們去initialize資料,加入的`@PostConstruct`註解會幫助我們在執行專案時,自動去執行這段程式碼
關鍵點其實在
```java=28
public List<Product> showallProduct( ) {
return productDB;
}
```
此函數直接返回了`productDB`這個每個元素都是`Product`的**List**
DemoController 幫助我們控管url這件事,任何使用者只要使用get訪問了/showallProduct,就會執行這個showallProduct函數,他利用了已經被Autowird過的demoProductDAO來執行showallProduct這個函數
```java=22
@RequestMapping(value = "/showallProduct", method = RequestMethod.GET)
public List<Product> showallProduct(){
return demoProductDAO.showallProduct();
}
```
### 如何執行專案
在瀏覽器上輸入
http://localhost:8080/showallProduct
就可以得到對應的資料
## 專案結果截圖
