SpringBoot学习3-热部署热加载/请求反馈

本文介绍了在代码量增大导致重新运行速度变慢的情况下,如何使用Spring Boot的DevTools模块进行热加载以提高开发效率。通过在pom.xml中添加依赖并配置IDE,可以实现在修改代码后无需重启应用就能快速看到效果。同时,文章提供了一个名为StatusReponse的响应类的示例,方便在控制器中返回JSON响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

每次修改了代码都需要重新运行才可以生效,刚开始代码量少速度还能接受,后面代码量多了,重新运行的速度比较慢,所以需要使用到热加载。
关于热加载,有商业版的JRebel and XRebel插件可以使用,在插件中心安装完了重启IDE即可在右上角看见运行按钮,缺点就是要钱,也可以使用通用的devtools来实现,首先需要在pom里面加入依赖:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
	<optional>true</optional>
</dependency>

新版的IDE需要在设置里的高级设置开启自动make:
在这里插入图片描述

接着,我们可以运行项目,然后修改代码后,即可点击右上角小锤子来进行热加载
在这里插入图片描述

请求反馈

统一新建一个类,StatusReponse

package com.example.demo.controller;

import lombok.Data;

@Data
public class StatusReponse {

    //是否成功
    private boolean success;
    //状态码
    private int code;
    //状态消息
    private String msg;
    //附加数据
    private Object data;

    StatusReponse(){};

    /**
     * 成功反馈
     * @return StatusReponse
     */
    public static StatusReponse success()
    {
        StatusReponse ajaxResponse = new StatusReponse();
        ajaxResponse.setCode(200);
        ajaxResponse.setSuccess(true);
        ajaxResponse.setMsg("查询成功");
        return ajaxResponse;
    }

    public static StatusReponse success(String msg,int code)
    {
        StatusReponse StatusReponse = new StatusReponse();
        StatusReponse.setCode(code);
        StatusReponse.setSuccess(true);
        StatusReponse.setMsg(msg);
        return StatusReponse;
    }

    public static StatusReponse success(Object data,String msg,int code)
    {
        StatusReponse StatusReponse = new StatusReponse();
        StatusReponse.setCode(code);
        StatusReponse.setData(data);
        StatusReponse.setSuccess(true);
        StatusReponse.setMsg(msg);
        return StatusReponse;
    }

    public static StatusReponse success(Object data,String msg)
    {
        StatusReponse StatusReponse = new StatusReponse();
        StatusReponse.setCode(200);
        StatusReponse.setData(data);
        StatusReponse.setSuccess(true);
        StatusReponse.setMsg(msg);
        return StatusReponse;
    }

    /**
     * 失败方法
     * @param code 状态码
     * @return StatusReponse
     */
    public static StatusReponse faild(int code)
    {
        StatusReponse StatusReponse = new StatusReponse();
        StatusReponse.setCode(code);
        StatusReponse.setSuccess(false);
        return StatusReponse;
    }

    public static StatusReponse faild(int code,String msg)
    {
        StatusReponse StatusReponse = new StatusReponse();
        StatusReponse.setCode(500);
        StatusReponse.setMsg(msg);
        StatusReponse.setSuccess(false);
        return StatusReponse;
    }

    public static StatusReponse faild()
    {
        StatusReponse StatusReponse = new StatusReponse();
        StatusReponse.setCode(500);
        StatusReponse.setSuccess(false);
        return StatusReponse;
    }
}

控制器使用StatusReponse.success(data,"查询成功");即可输出相关JSON。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值