写在前面的话,由于本人是一名移动端开发者,并未专业服务端开发人员,若有地方讲解不到位,或用词不当,还望广大好友谅解,写这个api接口就是纯属为了玩玩,看着后台的兄弟们挺有意思的,所以自己也过来玩一下,对自己以后也有帮助,下面写的接口可用于前端调用,或移动端调用,PostMan接口测试工具全部通过.

         开发环境准备,IDEA Intelligent、SpringBoot、Tomcat、数据库采用MySql

 SpringBoot的集成就不再讲解了,百度一大把,直接步入正题,api接口返回示例全部采用application/json数据格式,本文只说一下返回json格式的,其他格式后期有时间在补充,类注解@RestController,方法注解@ResponseBody

请求方式

@RequestMapping :自适应请求方式,也就是说同时支持GET和POST请求

@GetMapping :只能适用于GET请求

@PostMapping :只能适用于POST请求

@DeleteMapping: 客户端只能用 DELETE方式请求,使用于删除数据。

@PutMapping :客户端只能用 PUT方式请求,使用于修改数据(但在实际使用中,我个人建议还是采用POST方式较为妥当)。 

package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.bean.UserInfo;
import com.example.demo.dao.UserDao;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author ZJC
 * @Date 2019\7\17 0017 11:45
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/user")
public class UserController {

    /**
     * 用户登录
     *
     * @param name     用户账号
     * @param password 用户密码
     * @return
     */
    @RequestMapping("/login")
    public String postLogin(@RequestParam(name = "name",
            required = true,
            defaultValue = "") String name, @RequestParam(name = "password",
            required = true,
            defaultValue = "") String password) {
        System.out.println("name:" + name + ",password:" + password);
        JSONObject jsonObject = new JSONObject();
        String code = "";
        String msg = "";
        if (name == null || password == null || name.equals("") || password.equals("")) {
            code = "101";
            msg = "缺少字段";
        } else {
            UserInfo userInfo = null;
            try {
                userInfo = new UserDao().findUserByNameTest(name);
                if (userInfo == null) {
                    code = "101";
                    msg = "登录失败,该账号还未注册";
                } else {
                    if (userInfo.getPassword().equals(password)) {
                        code = "100";
                        msg = "登录成功";
                    } else {
                        code = "101";
                        msg = "登录失败,密码输入错误";
                    }
                }
            } catch (IOException e) {
                code = "101";
                msg = "登录失败,数据库查询异常:" + e.toString();
            }
        }
        jsonObject.put("code", code);
        jsonObject.put("msg", msg);
        return jsonObject.toString();
    }

    /**
     * 用户注册
     *
     * @param name     用户账号
     * @param password 用户密码
     * @return
     */
    @RequestMapping("/register")
    public String postRegister(@RequestParam(name = "name",
            required = true,
            defaultValue = "") String name, @RequestParam(name = "password",
            required = true,
            defaultValue = "") String password) {
        System.out.println("name:" + name + ",password:" + password);
        JSONObject jsonObject = new JSONObject();
        String code = "";
        String msg = "";
        if (name == null || password == null || name.equals("") || password.equals("")) {
            code = "101";
            msg = "缺少字段";
        } else if (password.length() < 6) {
            code = "101";
            msg = "注册失败,密码不能小于6位";
        } else {
            UserInfo userInfo = null;
            try {
                userInfo = new UserDao().findUserByNameTest(name);
                if (userInfo == null) {//说明账号不存在,则进行注册
                    int index = new UserDao().insertUserInfo(new UserInfo(name, password));
                    if (index != -1) {
                        code = "100";
                        msg = "注册成功";
                    } else {
                        code = "1001";
                        msg = "注册失败";
                    }
                } else {
                    code = "101";
                    msg = "注册失败,您的账号已注册,无需再次注册!";
                }
            } catch (IOException e) {
                code = "101";
                msg = "注册失败,数据库查询异常:" + e.toString();
            }
        }
        jsonObject.put("code", code);
        jsonObject.put("msg", msg);
        return jsonObject.toString();
    }

    /**
     * 修改密码
     *
     * @param name          用户账号
     * @param password      当前密码
     * @param resetpassword 更换后的密码
     * @return
     */
    @RequestMapping("/resetpsw")
    public String postResetPsw(@RequestParam(name = "name",
            required = true,
            defaultValue = "") String name, @RequestParam(name = "password",
            required = true,
            defaultValue = "") String password, @RequestParam(name = "resetpassword",
            required = true,
            defaultValue = "") String resetpassword) {


        System.out.println("name:" + name + ",password:" + password + ",resetpassword:" + resetpassword);
        JSONObject jsonObject = new JSONObject();
        String code = "";
        String msg = "";
        if (name == null || password == null || name.equals("") || password.equals("") || resetpassword == null || resetpassword.equals("")) {
            code = "101";
            msg = "缺少字段";
        } else if (resetpassword.length() < 6) {
            code = "101";
            msg = "重置失败,新密码不能小于6位";
        } else {
            UserInfo userInfo = null;
            try {
                userInfo = new UserDao().findUserByNameTest(name);
                if (userInfo != null) {//说明账号不存在,则进行注册
                    if (userInfo.getPassword().equals(password)) {
                        // TODO: 2019\7\17 0017 去修改
                        UserInfo info = new UserDao().updateUserInfoName(userInfo.getId(), userInfo.getUser_name(), resetpassword);
                        if (info.getId() != -1) {
                            code = "100";
                            msg = "重置成功";
                        } else {
                            code = "101";
                            msg = "重置失败";
                        }
                    } else {
                        // TODO: 2019\7\17 0017 旧密码输入错误
                        code = "101";
                        msg = "重置失败,您的旧密码输入错误";
                    }
                } else {
                    code = "101";
                    msg = "重置失败,您的账号未注册!";
                }
            } catch (IOException e) {
                code = "101";
                msg = "重置失败,数据库查询异常:" + e.toString();
            }
        }
        jsonObject.put("code", code);
        jsonObject.put("msg", msg);
        return jsonObject.toString();
    }
}

只要运行SpringBoot主类就跑起来了,在客户端和前端均可调用,这里在PostMan测试工具测试了一下修改密码的接口,另外两个接口同这个类似

基于spring boot的api接口签名认证 springboot写api接口_重置

数据库结构

基于spring boot的api接口签名认证 springboot写api接口_spring_02

以上是自己的小小经历,写的不怎么好,以后有机会往后台方面发展发展!

github:传送门