• Springboot01入门


    目录

    一、了解Springboot

    二、Springboot使用

     三、5.统一响应类及错误编码类


    一、了解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框架的一站式解决方案

    二、Springboot使用

    首先创建一个Springboot项目

    步骤如下:联网操作

    #可以换源
    http://start.aliyun.com 

    #默认勾选web、lombok、Mybatis组件

     

     

     

     

     

     

     

    项目创建成功

     

     

     项目结构:

    static 静态资源如js、img等

    template:模板引擎相对于maven项目里的webapp

    springboot默认是不支持jsp界面跳转的

    application.properties;springboot中唯二的配置文件

     实践新建一个控制类IndexController 

    1. package com.zking.springboot001;
    2. import org.springframework.web.bind.annotation.RequestMapping;
    3. import org.springframework.web.bind.annotation.RestController;
    4. /**
    5. * @author 锦鲤
    6. * @site www.lucy.com
    7. * @company xxx公司
    8. * @create  2022-10-28 18:30
    9. *
    10. * @RestController 返回json串
    11. */
    12. @RestController
    13. public class IndexController {
    14. @RequestMapping("/")
    15. public String index(){
    16. System.out.println("come");
    17. return "index";
    18. }
    19. }

    测试

    1. package com.zking.springboot001;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class Springboot001Application {
    6. public static void main(String[] args) {
    7. SpringApplication.run(Springboot001Application.class, args);
    8. }
    9. }

    效果

     


     再新建一个项目Springboot02

     

    不同处;

     

     

    1. package com.zking.springboot02.web;
    2. import org.springframework.web.bind.annotation.RequestMapping;
    3. import org.springframework.web.bind.annotation.RestController;
    4. /**
    5. * @author 锦鲤
    6. * @site www.lucy.com
    7. * @company xxx公司
    8. * @create  2022-10-28 18:30
    9. *
    10. * @RestController 返回json串
    11. */
    12. @RestController
    13. public class IndexController {
    14. @RequestMapping("/")
    15. public String index(){
    16. System.out.println("come");
    17. return "index";
    18. }
    19. }

    运行时报错:

     

     

     

    package com.zking.springboot02;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    //解决方法:
    @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
    public class Springboot02Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Springboot02Application.class, args);
        }
    
    }
    

     

    安装 Convert YAML and Properties File 的插件

    application.properties

     # 应用名称
    spring.application.name=testspringboot02
    # 应用服务 WEB 访问端口
    server.port=8081
    #下面这些内容是为了让MyBatis映射
    #指定Mybatis的Mapper文件
    mybatis.mapper-locations=classpath:mappers/*xml
    #指定Mybatis的实体目录
    mybatis.type-aliases-package=com.zking.testspringboot02.mybatis.entity

     选中application.properties右键点击 Convert YAML and Properties File

     

     调整格式如下

    1. server:
    2. port: 8080
    3. servlet:
    4. context-path: /lx
    5. spring:
    6. activemq:
    7. pool:
    8. block-if-full: false
    9. application:
    10. name: springboot001
    11. cache:
    12. infinispan:
    13. config: 'classpath*:'


     三、5.统一响应类及错误编码类

    测试代码

    1. package com.zking.springboot001;
    2. import jdk.nashorn.internal.objects.annotations.Constructor;
    3. import lombok.AllArgsConstructor;
    4. import lombok.Data;
    5. import org.springframework.web.bind.annotation.*;
    6. import java.util.Arrays;
    7. import java.util.HashMap;
    8. import java.util.List;
    9. import java.util.Map;
    10. /**
    11. * @author 锦鲤
    12. * @site www.lucy.com
    13. * @company xxx公司
    14. * @create  2022-10-28 19:31
    15. */
    16. @RestController
    17. @RequestMapping("/book")
    18. public class BookController {
    19. // @RequestMapping("/list")
    20. // public Map list(){
    21. // Map map=new HashMap();
    22. // map.put("msg","成功");
    23. // map.put("code",200);
    24. // return map;
    25. // }
    26. /* @GetMapping("list")
    27. public Map list(){}
    28. */
    29. @PutMapping("/add")
    30. public Map add(){
    31. Map map=new HashMap();
    32. map.put("msg","增加成功");
    33. map.put("code",200);
    34. return map;
    35. }
    36. @PostMapping("/update")
    37. public Map update(){
    38. Map map=new HashMap();
    39. map.put("msg","修改成功");
    40. map.put("code",200);
    41. return map;
    42. }
    43. @DeleteMapping("/del")
    44. public Map del(){
    45. Map map=new HashMap();
    46. map.put("msg","删除成功");
    47. map.put("code",200);
    48. return map;
    49. }
    50. //查询单个
    51. @RequestMapping("/load")
    52. public Map load(){
    53. Map map=new HashMap();
    54. map.put("data",new Book(1,"活着"));
    55. map.put("msg","查询成功");
    56. map.put("code",200);
    57. return map;
    58. }
    59. // 查询全部
    60. @RequestMapping("/list")
    61. public Map list(){
    62. List<Book> books= Arrays.asList(
    63. new Book(1,"红楼梦"),
    64. new Book(2,"水浒传"),
    65. new Book(3,"西游记"),
    66. new Book(4,"三国演义"));
    67. Map map=new HashMap();
    68. map.put("data",books);
    69. map.put("total",122);
    70. map.put("msg","查询成功");
    71. map.put("code",200);
    72. return map;
    73. }
    74. }
    75. @AllArgsConstructor
    76. @Data
    77. class Book{
    78. private int id;
    79. private String name;
    80. }

    进行测试

    package com.zking.springboot001;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Springboot001Application {
    
        public static void main(String[] args) {
    
            SpringApplication.run(Springboot001Application.class, args);
    
        }
    
    
    
    }
    

    eolink的使用:更细致地调测后台数据接口

    企业常使用postman

    安装后注册账户并且测试各个方法

    修改

     

     增加

     删除

     

    查询全部

     

     查询书籍

     

     

    响应封装类

    Result.java

     

     CodeMsg.java

     

    package com.zking.springboot001.Result;
    
    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 + "]";
        }
    }

    BookController2 

    @RequestMapping("/book2")

    1. package com.zking.springboot001;
    2. import com.zking.springboot001.Result.Result;
    3. import lombok.AllArgsConstructor;
    4. import lombok.Data;
    5. import org.springframework.web.bind.annotation.*;
    6. import java.util.Arrays;
    7. import java.util.HashMap;
    8. import java.util.List;
    9. import java.util.Map;
    10. /**
    11. * @author 锦鲤
    12. * @site www.lucy.com
    13. * @company xxx公司
    14. * @create  2022-10-28 19:31
    15. */
    16. @RestController
    17. @RequestMapping("/book2")
    18. public class BookController2 {
    19. @PutMapping("/add")
    20. public Result add(Book book){
    21. return Result.ok(200,"增加成功");
    22. }
    23. @PostMapping("/update")
    24. public Result update(){
    25. return Result.ok(200,"修改成功");
    26. }
    27. @DeleteMapping("/del")
    28. public Result del(int bid){
    29. return Result.ok(200,"删除成功");
    30. }
    31. //查询单个
    32. @RequestMapping("/load")
    33. public Result load(){
    34. return Result.ok(200,"加载成功");
    35. }
    36. // 查询全部
    37. @RequestMapping("/list")
    38. public Result list(){
    39. List books= Arrays.asList(
    40. new Book(1,"红楼梦"),
    41. new Book(2,"水浒传"),
    42. new Book(3,"西游记"),
    43. new Book(4,"三国演义"));
    44. return Result.ok(200,"查询成功",books,222);
    45. }
    46. }

    例如会出现的错误

    @PutMapping("/add")
    public Result add(Book book){
        int id=book.getId();
        if(id==0){
        Map map=new HashMap();
        map.put("错误msg","id未传递");
        }
        return  Result.ok(200,"增加成功");
    }
    1. //通用的错误码
    2. public static CodeMsg SUCCESS = new CodeMsg(200, "success");
    3. public static CodeMsg SERVER_ERROR = new CodeMsg(500, "服务端异常");
    4. public static CodeMsg BIND_ERROR = new CodeMsg(500101, "参数校验异常:%s");
    5. public static CodeMsg REQUEST_ILLEGAL = new CodeMsg(500102, "请求非法");
    6. public static CodeMsg ACCESS_LIMIT_REACHED = new CodeMsg(500104, "访问太频繁!");
    7. //登录模块5002XX
    8. public static CodeMsg SESSION_ERROR = new CodeMsg(500210, "Session不存在或者已经失效");
    9. public static CodeMsg PASSWORD_EMPTY = new CodeMsg(500211, "登录密码不能为空");
    10. public static CodeMsg MOBILE_EMPTY = new CodeMsg(500212, "手机号不能为空");
    11. public static CodeMsg MOBILE_ERROR = new CodeMsg(500213, "手机号格式错误");
    12. public static CodeMsg MOBILE_NOT_EXIST = new CodeMsg(500214, "手机号不存在");
    13. public static CodeMsg PASSWORD_ERROR = new CodeMsg(500215, "密码错误");


     

  • 相关阅读:
    机器内存充足,Java程序却报native内存OOM的问题记录
    【牛客刷题】——Python入门 06 条件语句
    甘露糖-聚乙二醇-羧酸|mannose-PEG-COOH|羧酸-PEG-甘露糖
    (LeetCode)两数相加深入分析Java版
    手摸手系列之 - 什么是接口的幂等性以及 AOP+Redis 基于注解实现接口幂等性校验
    技术学习:Python(21)|爬虫篇|selenium自动化操作浏览器
    对拍系列 v1.0
    Symfony 请求教程
    【Django】RESTful API接口设计风格
    〖Python 数据库开发实战 - Redis篇③〗- Mac系统下通过homebrew安装Redis数据库
  • 原文地址:https://blog.csdn.net/qq_66924116/article/details/127577186