2.springboot入门

简介: 2.springboot入门

1.简介

SpringBoot是什么

Spring Boot它本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速、敏捷地开发新一代基于Spring框架的应用程序。
也就是说,它并不是用来替代Spring的解决方案,而是和Spring框架紧密结合用于提升Spring开发者体验的工具

同时它集成了大量常用的第三方库配置(例如Jackson, JDBC, Mongo, Redis, Mail等等),
Spring Boot应用中这些第三方库几乎可以零配置的开箱即用(out-of-the-box),大部分的Spring Boot应用都
只需要非常少量的配置代码,开发者能够更加专注于业务逻辑

注1:敏捷式开发

注2:spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,

就像maven整合了所有的jar包,spring boot整合了所有的框架

注3:基于Spring框架的一站式解决方案

2.入门

2.1.创建项目

2.2.启动项目

出现错误

配置默认不加载数据源

package com.zjzaki.spboot02;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) //这里是改动
public class Spboot02Application {

    public static void main(String[] args) {
        SpringApplication.run(Spboot02Application.class, args);
    }
}

此时再启动, 启动成功

2.3.添加实例

web层

package com.zjzaki.spboot02.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {
    @RequestMapping("/")
    public String index(){
        System.out.println("进入测试....");
        return "index";
    }
}

重启项目

2.4.响应封装类

Result.java

package com.zjzaki.spboot02.web;

public class Result<T> {
 
    private int code;
    private String msg;
    //因为返回的数据不知道是什么类型,所以定义一个泛型
    private T data;
    private long total;

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    /**
     * 成功的时候调用
     */
    public static <T> Result<T> ok(T data) {
        return new Result<T>(data);
    }
    public static <T> Result<T> ok(int code,String msg) {
        return new Result<>(code,msg);
    }
    public static <T> Result<T> ok(int code,String msg,T data) {
        Result<T> success = ok(data);
        success.setCode(code);
        success.setMsg(msg);
        return success;
    }
    public static <T> Result<T> ok(int code,String msg,T data,long total) {
        Result<T> success = ok(code,msg,data);
        success.setTotal(total);
        return success;
    }
 
    /**
     * 失败的时候调用
     */
    public static <T> Result<T> error(CodeMsg codeMsg) {
        return new Result<T>(codeMsg);
    }
 
    private Result(T data) {
        this.data = data;
    }
 
    private Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
 
    private Result(CodeMsg codeMsg) {
        if (codeMsg != null) {
            this.code = codeMsg.getCode();
            this.msg = codeMsg.getMsg();
        }
    }
 
    public int getCode() {
        return code;
    }
 
    public void setCode(int code) {
        this.code = code;
    }
 
    public String getMsg() {
        return msg;
    }
 
    public void setMsg(String msg) {
        this.msg = msg;
    }
 
    public T getData() {
        return data;
    }
 
    public void setData(T data) {
        this.data = data;
    }
}

CodeMsg.java

package com.zjzaki.spboot02.web;

public class CodeMsg {
 
    private int code;
    private String msg;
 
    //通用的错误码
    public static CodeMsg SUCCESS = new CodeMsg(200, "success");
    public static CodeMsg SERVER_ERROR = new CodeMsg(500, "服务端异常");
    public static CodeMsg BIND_ERROR = new CodeMsg(500101, "参数校验异常:%s");
    public static CodeMsg REQUEST_ILLEGAL = new CodeMsg(500102, "请求非法");
    public static CodeMsg ACCESS_LIMIT_REACHED = new CodeMsg(500104, "访问太频繁!");
    //登录模块5002XX
    public static CodeMsg SESSION_ERROR = new CodeMsg(500210, "Session不存在或者已经失效");
    public static CodeMsg PASSWORD_EMPTY = new CodeMsg(500211, "登录密码不能为空");
    public static CodeMsg MOBILE_EMPTY = new CodeMsg(500212, "手机号不能为空");
    public static CodeMsg MOBILE_ERROR = new CodeMsg(500213, "手机号格式错误");
    public static CodeMsg MOBILE_NOT_EXIST = new CodeMsg(500214, "手机号不存在");
    public static CodeMsg PASSWORD_ERROR = new CodeMsg(500215, "密码错误");
 
    //商品模块5003XX
 
    //订单模块5004XX
    public static CodeMsg ORDER_NOT_EXIST = new CodeMsg(500400, "订单不存在");
 
    //秒杀模块5005XX
    public static CodeMsg MIAO_SHA_OVER = new CodeMsg(500500, "商品已经秒杀完毕");
    public static CodeMsg REPEATE_MIAOSHA = new CodeMsg(500501, "不能重复秒杀");
    public static CodeMsg MIAOSHA_FAIL = new CodeMsg(500502, "秒杀失败");
 
    private CodeMsg() {
    }
 
    private CodeMsg(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
 
    public int getCode() {
        return code;
    }
 
    public void setCode(int code) {
        this.code = code;
    }
 
    public String getMsg() {
        return msg;
    }
 
    public void setMsg(String msg) {
        this.msg = msg;
    }
 
    public CodeMsg fillArgs(Object... args) {
        int code = this.code;
        String message = String.format(this.msg, args);
        return new CodeMsg(code, message);
    }
 
    @Override
    public String toString() {
        return "CodeMsg [code=" + code + ", msg=" + msg + "]";
    }
}

DemoController2.java

package com.zjzaki.spboot02.web;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/demo2")
public class DemoController2 {
    @GetMapping("/list")
    public Result list(){
        return Result.ok(200,"查询成功",
                Arrays.asList(
                new Demo(1,"足球"),
                new Demo(2,"篮球"),
                new Demo(3,"排球"),
                new Demo(4,"羽毛球"),
                new Demo(5,"台球")
        ),168);
    }

    @PostMapping("/add")
    public Result add(){
        return Result.ok(200,"新增成功");
    }

    @PostMapping("/load")
    public Result load(){
        return Result.ok(200,"加载成功",new Demo(3,"排球"));
    }


    @PutMapping("/edit")
    public Result edit(){
        return Result.ok(200,"修改成功");
    }

    @DeleteMapping("/delete")
    public Result delete(){
        return Result.ok(200,"删除成功");
    }

    @GetMapping("/queryByUser")
    public Result queryByUser(){
        return Result.error(CodeMsg.BIND_ERROR.fillArgs("密码等级太低"));
    }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Demo{
    private int id;
    private String name;
}

请求http://localhost:8080/demo2/list 正常访问

访问http://localhost:8080/demo2/add 出现405错误

此时需要用到一个工具

3.Eolink安装与使用

3.1.双击安装文件

点击下一步

3.2.选择安装位置

3.3.点击安装,等待自动更新完成

3.4.扫码登录

3.5.选择快速测试

3.6.后台数据接收

修改DemoController2的add方法

@PostMapping("/add")
public Result add(Demo demo, @RequestBody Map map,@RequestHeader("jwt") String jwt){
    System.out.println(demo);
    System.out.println(map);
    System.out.println(jwt);
    return Result.ok(200,"新增成功");
}

控制台打印

4.安装 Convert YAML and Properties File 的插件

Properties在配置项很多是不便于查看,此时我们需要yaml文件来进行配置(能够有层次感, 方便查看)

目录
相关文章
|
2月前
|
Java 应用服务中间件 nginx
SpringBoot入门(九)
SpringBoot入门(九)
|
2月前
|
NoSQL Java Redis
SpringBoot的入门(一)
SpringBoot的入门(一)
|
2月前
|
Java 容器 Spring
SpringBoot入门(六)
SpringBoot入门(六)
|
2月前
|
Java Spring 容器
SpringBoot的入门(三)
SpringBoot的入门(三)
|
2月前
|
Java API 容器
SpringBoot入门(十)
SpringBoot入门(十)
|
2月前
|
消息中间件 NoSQL Java
SpringBoot的入门(五)
SpringBoot的入门(五)
|
2月前
|
监控 Java Spring
SpringBoot的入门(四)
SpringBoot的入门(四)
|
2月前
|
JSON Java 数据格式
SpringBoot入门(七)
SpringBoot入门(七)
|
5月前
|
Java 数据库连接 Maven
|
5月前
|
Java Maven