Spring boot实现RESTful接口

移动端开发经常接触到后端API,使用Java实现一个RESTful Web API

安装Visual Sutdio Code

安装好VS Code后,需要安装以下插件:

  1. Extension Pack for Java。
  2. Spring boot Extension Pack。

新建项目

  1. 使用快捷键(Ctrl+Shift+P)命令窗口,输入Spring选择创建Gradle(Maven) 项目。或者,左侧资源管理器中,点击“创建Java项目”,输入Spring,选择Spring Boot,创建Gradle(Maven) 项目。
  2. 选择Spring boot版本,2.6.3,任意选。
  3. 选择开发语言,Java,也可以选择其他语言。
  4. 输入项目包名com.example.api,自己取。
  5. 输入项目名demo,自己取。
  6. 选择打包方式War,也可选另一种Jar
  7. 指定JDK版本。
  8. 选择依赖库,Spring Web

回车,到这里已经自动生成了一个项目,打开项目,build.gradle文件中可以修改配置。

实现一个文件上传的Controller

文件保存和主机文件系统有关系,本示例是Mac系统,Linux系统也可用。

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");
    }
}

响应父类

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/textupload/formVS Code运行项目后,可以以内容方式或者表单方式上传文件。

例如:
url1 = "http://localhost:8080/upload/text"
url2 = "http://localhost:8080/upload/form"

部署

前面已经可以在开发环境运行,如果需要部署到主机或者服务器上正在运行的Tomcat容器,则需要生成WarJar包。

生成资源包:

./gradlew build

运行上面指令后,在项目的build/libs目录下生成的资源包,拷贝出来放置Tomcat下的webapps目录,即可自动运行。

注意:
部署后的url需要加上war资源包的名称,即项目名。
url1 = "http://localhost:8080/demo/upload/text"
url2 = "http://localhost:8080/demo/upload/form"

数据库

添加数据库处理能力,模拟实现用户注册,登录。

1. 数据库安装和设置

安装Mysql

Mac: brew install mysql
Linux: apt-get install mysql 或 yun install mysql

创建demodb数据库

登录root用户,默认空密码:mysql -u root -p
创建数据库:create database demodb;

新建数据库访问用户test,并设置权限

创建用户:create user 'test'@'localhost' identified by '123456';
授权'demo'数据库的所有操作:grant all privileges on demodb.* to 'test'@'localhost';

localhost表示限制为本机访问,也可指定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;

数据库名demodb,用户名test和密码123456一会需要用到。

2. 项目配置

首先,需要在build.gradle文件中添加两个依赖:

    //Java持久层接口,ORM框架,类似移动端Room,GreenDao,Realm
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    //mysql 数据库驱动
    implementation 'mysql:mysql-connector-java'

其次,在resources文件夹下的application.properties文件中添加如下设置:

#驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#链接的数据库地址,端口和名称
spring.datasource.url=jdbc:mysql://localhost:3306/demodb
#登录用户名
spring.datasource.username=test
#登录密码
spring.datasource.password=123456
#第一次启动根据实体建立表结构,之后启动会根据实体的改变更新表结构
spring.jpa.hibernate.ddl-auto=update
#打印Sql
spring.jpa.show-sql=true
#禁用
spring.jpa.open-in-view=false

3. 登录注册接口实现

User对象,即数据库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;
    
    //查询必须有无参构造
    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,数据库操作接口。

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 {

    @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控制器实现。

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");
        }
    }
}

你可能感兴趣的:(Spring boot实现RESTful接口)