移動端開發(fā)經(jīng)常接觸到后端API
,使用Java
實現(xiàn)一個RESTful Web API
昵宇。
安裝Visual Sutdio Code
安裝好VS Code
后,需要安裝以下插件:
- Extension Pack for Java形葬。
- Spring boot Extension Pack竖幔。
新建項目
- 使用快捷鍵(
Ctrl+Shift+P
)命令窗口,輸入Spring
選擇創(chuàng)建Gradle
(Maven
) 項目砸脊【咂或者,左側(cè)資源管理器中凌埂,點擊“創(chuàng)建Java項目”驱显,輸入Spring
,選擇Spring Boot
,創(chuàng)建Gradle
(Maven
) 項目埃疫。 - 選擇
Spring boot
版本伏恐,2.6.3
,任意選栓霜。 - 選擇開發(fā)語言翠桦,
Java
,也可以選擇其他語言胳蛮。 - 輸入項目包名
com.example.api
秤掌,自己取。 - 輸入項目名
demo
鹰霍,自己取闻鉴。 - 選擇打包方式
War
,也可選另一種Jar
茂洒。 - 指定
JDK
版本孟岛。 - 選擇依賴庫,
Spring Web
督勺。
回車渠羞,到這里已經(jīng)自動生成了一個項目,打開項目智哀,build.gradle
文件中可以修改配置次询。
實現(xiàn)一個文件上傳的Controller
文件保存和主機(jī)文件系統(tǒng)有關(guān)系,本示例是Mac
系統(tǒng)瓷叫,Linux
系統(tǒng)也可用屯吊。
package com.example.test.demo.controllers;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import com.example.test.demo.bean.BaseResponse;
import com.example.test.demo.utils.FileUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping(path = "/upload")
public class UploadController {
static String rootDir = "/Users/wacdgit/files";
@PostMapping(path = "/text", consumes = MediaType.TEXT_PLAIN_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public BaseResponse uploadText(@RequestBody String text) {
if (text == null) {
return new BaseResponse(-1, "Text is null");
}
File dir = new File(rootDir);
if (!dir.exists()) {
dir.mkdirs();
}
FileUtils.saveText(text, new File(rootDir, "temp.md"));
return new BaseResponse(0, "Success");
}
@PostMapping(path = "/form", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public BaseResponse uploadForm(@RequestParam("file") MultipartFile multipartFile) {
InputStream ins = null;
try {
ins = multipartFile.getInputStream();
File dir = new File(rootDir);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(rootDir, multipartFile.getOriginalFilename());
FileUtils.saveFile(ins, file);
} catch (Exception e1) {
e1.printStackTrace();
return new BaseResponse(-1, e1.getMessage());
} finally {
if (ins != null) {
try {
ins.close();
} catch (IOException e2) {
e2.printStackTrace();
return new BaseResponse(-2, e2.getMessage());
}
}
}
return new BaseResponse(0, "Success");
}
}
響應(yīng)父類
package com.example.test.demo.bean;
import java.beans.JavaBean;
@JavaBean
public class BaseResponse {
private int error;
private String msg;
public BaseResponse() {
}
public BaseResponse(int error, String msg) {
this.error = error;
this.msg = msg;
}
public int getError() {
return error;
}
public void setError(int error) {
this.error = error;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
兩個RESTful API
接口分別為upload/text
和upload/form
。VS Code
運(yùn)行項目后摹菠,可以以內(nèi)容方式或者表單方式上傳文件盒卸。
例如:
url1 = "http://localhost:8080/upload/text"
url2 = "http://localhost:8080/upload/form"
部署
前面已經(jīng)可以在開發(fā)環(huán)境運(yùn)行,如果需要部署到主機(jī)或者服務(wù)器上正在運(yùn)行的Tomcat
容器次氨,則需要生成War
或Jar
包蔽介。
生成資源包:
./gradlew build
運(yùn)行上面指令后,在項目的build/libs
目錄下生成的資源包煮寡,拷貝出來放置Tomcat
下的webapps
目錄虹蓄,即可自動運(yùn)行。
注意:
部署后的url需要加上war資源包的名稱幸撕,即項目名薇组。
url1 = "http://localhost:8080/demo/upload/text"
url2 = "http://localhost:8080/demo/upload/form"
數(shù)據(jù)庫
添加數(shù)據(jù)庫處理能力,模擬實現(xiàn)用戶注冊杈帐,登錄体箕。
1. 數(shù)據(jù)庫安裝和設(shè)置
安裝Mysql
Mac: brew install mysql
Linux: apt-get install mysql 或 yun install mysql
創(chuàng)建demodb
數(shù)據(jù)庫
登錄root用戶专钉,默認(rèn)空密碼:mysql -u root -p
創(chuàng)建數(shù)據(jù)庫:create database demodb;
新建數(shù)據(jù)庫訪問用戶test
挑童,并設(shè)置權(quán)限
創(chuàng)建用戶:create user 'test'@'localhost' identified by '123456';
授權(quán)'demo'數(shù)據(jù)庫的所有操作:grant all privileges on demodb.* to 'test'@'localhost';
localhost
表示限制為本機(jī)訪問累铅,也可指定IP地址,或%
替代表示允許所有地址訪問站叼。
新建用戶表user
也可以不做這一步娃兽,JPA
框架可以自動生成表。
USE demodb;
CREATE TABLE IF NOT EXISTS user(
id INT UNSIGNED AUTO_INCREMENT,
name VARCHAR(32) NOT NULL,
password VARCHAR(20) NOT NULL,
PRIMARY KEY(id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
DESC user;
數(shù)據(jù)庫名demodb
尽楔,用戶名test
和密碼123456
一會需要用到投储。
2. 項目配置
首先,需要在build.gradle
文件中添加兩個依賴:
//Java持久層接口阔馋,ORM框架玛荞,類似移動端Room,GreenDao呕寝,Realm
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
//mysql 數(shù)據(jù)庫驅(qū)動
implementation 'mysql:mysql-connector-java'
其次勋眯,在resources
文件夾下的application.properties
文件中添加如下設(shè)置:
#驅(qū)動
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#鏈接的數(shù)據(jù)庫地址,端口和名稱
spring.datasource.url=jdbc:mysql://localhost:3306/demodb
#登錄用戶名
spring.datasource.username=test
#登錄密碼
spring.datasource.password=123456
#第一次啟動根據(jù)實體建立表結(jié)構(gòu)下梢,之后啟動會根據(jù)實體的改變更新表結(jié)構(gòu)
spring.jpa.hibernate.ddl-auto=update
#打印Sql
spring.jpa.show-sql=true
#禁用
spring.jpa.open-in-view=false
3. 登錄注冊接口實現(xiàn)
User
對象客蹋,即數(shù)據(jù)庫user
表。
package com.example.test.demo.dao;
import java.beans.JavaBean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user")
@JavaBean
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "name", length = 32, unique = true, nullable = false)
private String name;
@Column(name = "password", length = 20, nullable = false)
private String pwd;
//查詢必須有無參構(gòu)造
public User() {
}
public User(String name, String pwd) {
this.name = name;
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
UserDao
孽江,數(shù)據(jù)庫操作接口讶坯。
package com.example.test.demo.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
public interface UserDao extends JpaRepository<User, Integer> {
@Query(value = "select * from user where name=?", nativeQuery = true)
User findByName(String name);
@Query(value = "select * from user where name=?1 and password=?2", nativeQuery = true)
User findByNameAndPwd(String name, String pwd);
}
UserController
控制器實現(xiàn)。
package com.example.test.demo.controllers;
import com.example.test.demo.bean.BaseResponse;
import com.example.test.demo.dao.User;
import com.example.test.demo.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(path = "/user")
public class UserController {
@Autowired
UserDao userDao;
/**
* POST register a user
*
* @param name
* @param pwd
* @return
*/
@PostMapping(path = "/register", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public BaseResponse register(String name, String pwd) {
if (name == null || name.isEmpty() || name.isBlank()) {
return new BaseResponse(-1, "The name can not be empty");
}
if (pwd == null || pwd.isEmpty() || pwd.isBlank()) {
return new BaseResponse(-2, "The pwd can not be empty");
}
User user = userDao.findByName(name);
if (user != null) {
return new BaseResponse(-3, "Exist user");
} else {
userDao.save(new User (name, pwd));
return new BaseResponse(0, "Success");
}
}
/**
* GET request岗屏,鏈接中帶用戶名和密碼
*
* @param name
* @param pwd
* @return
*/
@GetMapping(path = "/login")
public BaseResponse login1(@RequestParam(value = "name") String name, @RequestParam(value = "pwd") String pwd) {
return login(name, pwd);
}
@PostMapping(path = "/login", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public BaseResponse login2(String name, String pwd) {
return login(name, pwd);
}
/**
* POST request and response data format: Json
* User 需要@JavaBean注解
*
* @param user
* @return
*/
@PostMapping(path = "/login")
public BaseResponse login3(@RequestBody User user) {
return login(user.getName(), user.getPwd());
}
/**
* POST request and response data format: Json
* User 可以不使用@JavaBean注解
*
* @param user
* @return
*/
@PostMapping(path = "/login", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public BaseResponse login4(@RequestBody User user) {
return login(user.getName(), user.getPwd());
}
private BaseResponse login(String name, String pwd) {
if (name == null || name.isEmpty() || name.isBlank()) {
return new BaseResponse(-1, "The name can not be empty");
}
if (pwd == null || pwd.isEmpty() || pwd.isBlank()) {
return new BaseResponse(-2, "The pwd can not be empty");
}
User user = userDao.findByNameAndPwd(name, pwd);
if (user == null) {
return new BaseResponse(-3, "The user is not exist");
} else {
return new BaseResponse(0, "Success");
}
}
}