SpringBoot通用返回类AjaxResult用来返回状态码信息以及提示信息

/**
* @ClassName: AjaxResult
* @Description: TODO(ajax操作消息提醒)
* @author fuce
* @date 2018年8月18日
*
 */
public class AjaxResult extends HashMap<String, Object>
{
    private static final long serialVersionUID = 1L;

    /**
     * 初始化一个新创建的 Message 对象
     */
    public AjaxResult()
    {
    }

    /**
     * 返回错误消息
     * 
     * @return 错误消息
     */
    public static AjaxResult error()
    {
        return error(1, "操作失败");
    }

    /**
     * 返回错误消息
     * 
     * @param msg 内容
     * @return 错误消息
     */
    public static AjaxResult error(String msg)
    {
        return error(500, msg);
    }

    /**
     * 返回错误消息
     * 
     * @param code 错误码
     * @param msg 内容
     * @return 错误消息
     */
    public static AjaxResult error(int code, String msg)
    {
        AjaxResult json = new AjaxResult();
        json.put("code", code);
        json.put("msg", msg);
        return json;
    }

    /**
     * 返回成功消息
     * 
     * @param msg 内容
     * @return 成功消息
     */
    public static AjaxResult success(String msg)
    {
        AjaxResult json = new AjaxResult();
        json.put("code", 200);
        json.put("msg", msg);
        return json;
    }
    
    /**
     * 返回成功消息
     * 
     * @return 成功消息
     */
    public static AjaxResult success()
    {
        return AjaxResult.success("操作成功");
    }
    
    public static AjaxResult successData(int code, Object value){
    	 AjaxResult json = new AjaxResult();
    	 json.put("code", code);
         json.put("data", value);
         return json;
    }

    /**
     * 返回新增立项申报后的信息
     * @param code
     * @param msg
     * @param id
     * @return
     */
    public static AjaxResult successProjectData(int code, String msg, String id,String pId){
        AjaxResult json = new AjaxResult();
        json.put("code", code);
        json.put("msg", msg);
        json.put("id", id);
        json.put("pId",pId);
        return json;
    }



    /**
     * 返回项目基本信息
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static AjaxResult successProjectInfoData(int code, String msg, Object data){
        AjaxResult json = new AjaxResult();
        json.put("code", code);
        json.put("msg", msg);
        json.put("data", data);
        return json;
    }
    
    /**
     * 返回成功消息
     * 
     * @param key 键值
     * @param value 内容
     * @return 成功消息
     */
    @Override
    public AjaxResult put(String key, Object value)
    {
        super.put(key, value);
        return this;
    }
}

第一步:引入以上AjaxResult工具类即可,

/**
 * web层通用数据处理
* @ClassName: BaseController
* @author fuce
* @date 2018年8月18日
*
 */
@Controller
public class BaseController
{
    /**
     * 将前台传递过来的日期格式的字符串,自动转化为Date类型
     */
    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }


    /**
     * 响应返回结果
     * 
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(int rows)
    {
        return rows > 0 ? success() : error();
    }

    /**
     * 返回成功
     */
    public AjaxResult success()
    {
        return AjaxResult.success();
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error()
    {
        return AjaxResult.error();
    }

    /**
     * 返回成功消息
     */
    public AjaxResult success(String message)
    {
        return AjaxResult.success(message);
    }


    /**
     * 返回失败消息
     */
    public AjaxResult error(String message)
    {
        return AjaxResult.error(message);
    }

    /**
     * 返回错误码消息
     */
    public AjaxResult error(int code, String message)
    {
        return AjaxResult.error(code, message);
    }
    
    /**
     * 返回object数据
     */
    public AjaxResult retobject(int code, Object  data)
    {
        return AjaxResult.successData(code, data);
    }

    /**
     * 返回立项申报新增后的数据
     */
    public AjaxResult successProject(int code, String msg, String projectId, String pId)
    {
        return AjaxResult.successProjectData(code, msg, projectId, pId);
    }
}

第二部:在controller层中继承BaseController

第三步:更改下返回值类型

@ApiOperation注解:用来构建 swaggerApi接口文档,方便接口开发好后自己测试

@ResponseBody:返回json格式数据

AjaxResult:为封装好的工具类,用来返回状态码信息、提示信息 

返回成功 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
引用\[1\]中的代码展示了一个前后台的ajax交互过程。在这个例子中,前台使用了jQuery的ajax方法发送了一个GET请求到指定的URL,并传递了一个名为supplierId的参数。后台使用了Spring MVC的@RequestMapping注解来映射请求的URL,并通过HttpServletRequest对象获取了前台传递的参数。在后台的处理方法中,调用了supplierService的deleteSupplierById方法来删除对应的供应商,并将操作结果封装到一个名为Result的泛型中,并设置了success属性为true。最后,后台方法返回了这个Result对象,将其转换为JSON格式的数据返回给前台。 根据你的问题,ajaxresult的使用是指在这个例子中Result的使用。Result是一个泛型,用于封装操作结果。在这个例子中,Result的泛型参数是String型。Result中有一个success属性,用于表示操作是否成功。在后台方法中,通过调用rs.setSuccess(true)来设置success属性为true,表示操作成功。在前台的ajax的success回调函数中,通过解析返回JSON数据,获取到Result对象,并通过rs.success来判断操作是否成功。如果成功,弹出"删除成功!"的提示。 总结来说,ajaxresult的使用是通过Result来封装操作结果,并在前后台的交互中进行传递和解析。 #### 引用[.reference_title] - *1* [AjaxResult工具封装和使用](https://blog.csdn.net/wukai_1996/article/details/114580997)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值