当前位置:   article > 正文

springboot入门_java restcontroller import

java restcontroller import

一、简介

1、springboot是什么

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

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

2、springboot的特点

1:敏捷式开发
2:spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,
就像maven整合了所有的jar包,spring boot整合了所有的框架
3:基于Spring框架的一站式解决方案

二、入门

1、创建项目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、web层

package com.xnx.springboot01.demo;

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

/**
 * @author xnx
 * @create 2022-10-28 11:35
 */
//等价于 @responseBody + @controller
@RestController
public class IndexController {
    @RequestMapping("/")
    public String index(){
        System.out.println("come in");
//        index.jsp
//        index 字符串
        return  "index";
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

在这里插入图片描述
在这里插入图片描述

3、安装 Convert YAML and Properties File 的插件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三、响应封装类配置

1、原始用法

①BookController

package com.xnx.springboot01.demo;

import jdk.nashorn.internal.objects.annotations.Constructor;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xnx
 * @create 2022-10-28 18:12
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @RequestMapping("/list")
    public Map list(){
//        查询数据库
        List<Book> books = Arrays.asList(new Book(1, "西游记"),
                new Book(2, "三国演义"),
                new Book(3, "水浒传"),
                new Book(4, "红楼梦"));
        Map map = new HashMap();
        map.put("data",books);
        map.put("total",122);
        map.put("msg","查询成功");
        map.put("code",200);
        return map;
    }

    @RequestMapping("/add")
    public Map add(){
        Map map = new HashMap();
        map.put("msg","新增成功");
        map.put("code",200);
        return map;
    }

    @RequestMapping("/update")
    public Map update(){
        Map map = new HashMap();
        map.put("msg","修改成功");
        map.put("code",200);
        return map;
    }

    @RequestMapping("/delete")
    public Map delete(){
        Map map = new HashMap();
        map.put("msg","删除成功");
        map.put("code",200);
        return map;
    }

    @RequestMapping("/load")
    public Map load(){
        Map map = new HashMap();
        map.put("data",new Book(1,"西游记"));
        map.put("msg","查询成功");
        map.put("code",200);
        return map;
    }
}

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

测试:
在这里插入图片描述

②修改注解

package com.xnx.springboot01.demo;

import jdk.nashorn.internal.objects.annotations.Constructor;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xnx
 * @create 2022-10-28 18:12
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @GetMapping("/list")
    public Map list(){
//        查询数据库
        List<Book> books = Arrays.asList(new Book(1, "西游记"),
                new Book(2, "三国演义"),
                new Book(3, "水浒传"),
                new Book(4, "红楼梦"));
        Map map = new HashMap();
        map.put("data",books);
        map.put("total",122);
        map.put("msg","查询成功");
        map.put("code",200);
        return map;
    }

    @PutMapping("/add")
    public Map add(Book book){
        System.out.println(book);
        Map map = new HashMap();
        map.put("msg","新增成功");
        map.put("code",200);

        Map map1 = new HashMap();
        map.put("msg","id未传递");
        return map;
    }

    @PostMapping("/update")
    public Map update(){
        Map map = new HashMap();
        map.put("msg","修改成功");
        map.put("code",200);
        return map;
    }

    @DeleteMapping("/delete")
    public Map delete(int bid){
        System.out.println(bid);
        Map map = new HashMap();
        map.put("msg","删除成功");
        map.put("code",200);
        return map;
    }

    @RequestMapping("/load")
    public Map load(){
        Map map = new HashMap();
        map.put("data",new Book(1,"西游记"));
        map.put("msg","查询成功");
        map.put("code",200);
        return map;
    }
}

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80

在这里插入图片描述

③安装一个测试软件

测试修改注释后的代码,405为错误提示

测试无参的方法

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

测试带参的方法

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、响应封装类

①Result.java

package com.xnx.springboot01.result;

/**
 * @author xnx
 * @create 2022-10-28 18:46
 */
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;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91

②CodeMsg.java

package com.xnx.springboot01.result;

/**
 * @author xnx
 * @create 2022-10-28 18:47
 */
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 + "]";
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

③BookController2

package com.xnx.springboot01.demo;

import com.xnx.springboot01.result.CodeMsg;
import com.xnx.springboot01.result.Result;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xnx
 * @create 2022-10-28 18:12
 */
@RestController
@RequestMapping("/book2")
public class BookController2 {
    @GetMapping("/list")
    public Result list(){
//        查询数据库
        List<Book> books = Arrays.asList(new Book(1, "西游记"),
                new Book(2, "三国演义"),
                new Book(3, "水浒传"),
                new Book(4, "红楼梦"));
        return Result.ok(200,"查询成功",books,222);
    }

    @PutMapping("/add")
    public Result add(Book book){
        System.out.println(book);
        int id = book.getId();
        if(id == 0){
            Map map = new HashMap();
            map.put("msg","name未传递");
//            return Result.error(CodeMsg.BIND_ERROR.fillArgs("id未传递"));
        }
        return Result.ok(200,"新增成功");
    }

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

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

    @RequestMapping("/load")
    public Result load(){
        return Result.ok(200,"加载成功",new Book(5,"aaa"));
    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

两者结果一样,只不过后者的代码量比前者少

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/226938
推荐阅读
相关标签
  

闽ICP备14008679号