Coder Social home page Coder Social logo

springboot-restful-starter's Introduction

springboot-restful-starter

SpringBoot RESTful API 脚手架

简介

这是一个基于SpringBoot 2.1.1 RELEASE,用于搭建RESTful API工程的脚手架,只需三分钟你就可以开始编写业务代码,不再烦恼于构建项目与风格统一。

快速开始

  1. 构建数据库
  2. 运行/src/test下的CodeGenerator.java进行代码生成
  3. 开始编写业务代码

内置功能与使用方法

RESTful风格Result生成器

1.成功且不带数据的结果

// 不带数据的成功结果
return new Result().success();

返回结果示例:

{
    "code": 200,
    "message": "Success",
    "data": null
}

2.成功且带返回数据的结果

return new Result().success("Hello,world");

// 当然你也可以返回对象或其他类型的数据
User user = new User();
return new Result().success(user);

返回结果示例:

{
    "code": 200,
    "message": "Success",
    "data": "Hello,world"
}

或者是:

{
    "code": 200,
    "message": "Success",
    "data": {
        "name": "jack",
        "age": 20
    }
}

3.错误结果:

// fail方法的参数(错误代码,错误信息)
return new Result().fail(10400, "登陆失败,密码错误");

// 你还可以自定义错误结果的code
return new Result().fail(null, "未登录", 401);

返回结果示例:

{
    "code": 400,
    "message": "登陆失败,密码错误",
    "data": 10400
}

或者:

{
    "code": 401,
    "message": "未登录",
    "data": null
}

RESTful风格的异常接管

// 参数说明(错误信息, 错误Code)
throw new ServiceException("未登录", 401);

// 你也可以返回错误代码
throw new ServiceException(10404, "服务器维护中", 404);

返回结果示例:

{
    "code": 401,
    "message": "未登录",
    "data": null
}

或者:

{
    "code": 404,
    "message": "服务器维护中",
    "data": 10404
}

基于JWT的认证机制

@Autowired
private TokenService tokenService;

// 生成Payload
Map<String,Object> payload = new HashMap<String,Object>();
payload.put("id",1);
// 生成Token
tokenService.generate(TokenType.ACCESS, payload, 1);


// 格式化Token
String token = getYourToken();
tokenService.parse(token); // 返回的结果是一个Jwt对象,详见JJWT文档

Auth注解

Auth注解用于获取当前用户的Token中的userId,在获取的同时会自动校验用户Token,若用户未登录则会抛出未登录的异常。

// 在controller中使用
@PostMapping("/user/1/edit")
public Result edit(@Auth int userId, @RequestBody sthPosted) {
    // 根据ID判断权限
}

MybatisPlus集成

相关使用方法请参考官方文档

相关依赖

  • SpringBoot
  • Mybatis Plus
  • lombok
  • jjwt
  • fastjson
  • druid

捐赠

如果你觉得这个项目很赞且对你有帮助的话,请我喝一杯咖啡呗:) 捐赠二维码 当然你也可以领取支付宝红包 支付宝红包

License

MIT

springboot-restful-starter's People

Contributors

lmarklil avatar

Watchers

James Cloos avatar yiicode avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.