结果工具类使用Result、MsgResponse

文章介绍了在Java中用于规范化结果展示的两个工具类:Result和MsgResponse。Result类基于HashMap,用于封装HTTP状态码和自定义消息,提供静态方法方便构建成功或错误的响应。MsgResponse类则包含状态码、消息和数据字段,利用枚举ResponseCode来表示不同的响应状态,提供了构建成功或失败响应的静态方法。
摘要由CSDN通过智能技术生成

目的:结果展示规范化

1、Result 结果工具类

package com.文件地址.common;

import org.apache.http.HttpStatus;


import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 封装返回数据
 **/
public class Result extends HashMap<String, Object> {

	private static final long serialVersionUID = 1L;

	public Result() {
			put("code", 0);
	        put("msg", "success");
	}

	public static Result error() {
			return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常,请联系管理员");
    }

	public static Result error(String msg) {
        	return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

	public static Result error(int code, String msg) {
	        Result r = new Result();
	        r.put("code", code);
	        r.put("msg", msg);
	        return r;
    }

	public static Result ok(String msg) {
	        Result r = new Result();
	        r.put("msg", msg);
	        return r;
    }

	public static Result ok(Map<String, Object> map) {
	        Result r = new Result();
	        r.putAll(map);
	        return r;
    }

	public static Result ok() {
        	return new Result();
    }

	public Result put(String key, Object value) {
	        super.put(key, value);
	        return this;
	}
}



使用

    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public Result test(@RequestBody TestEntity testEntity){
        TestService.save(testEntity);
        return Result.ok();
    }
    
    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public Result test(@RequestParam Map<String,Object> params){
        //查询列表数据分页
        Query query = new Query(params);
        List<TestEntity> list = TestService.queryList(query);
        int total = TestService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
        
        return Result.ok().put("data", pageUtil);
    }

2、MsgResponse结果工具类

package cn.文件地址.common.domain;

import cn.文件地址.common.enums.base.ResponseCode;
import lombok.Data;

import java.io.Serializable;

/**
 * Description:
 * Author: 
 * Date: 2020/5/22 14:31
 */
@Data
public class MsgResponse<T> implements Serializable {
    private int code = ResponseCode.SUCCESS.getCode();
    private String message = ResponseCode.SUCCESS.getMessage();
    private T data;

    public MsgResponse() {
    }

    private MsgResponse(T data) {
        this.data = data;
    }

    private MsgResponse(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> MsgResponse<T> success(T data) {
        MsgResponse<T> result = new MsgResponse<>(data);
        result.setCode(ResponseCode.SUCCESS.getCode());
        result.setMessage(ResponseCode.SUCCESS.getMessage());
        return result;
    }

    public static <T> MsgResponse<T> fail(int code, String message) {
        return new MsgResponse<T>(code, message);
    }

}

package cn.文件地址.common.enums.base;

import lombok.Getter;

/**
 * Description:
 */
public enum ResponseCode {
    SUCCESS(200, "成功"),
    ERROR(500, "服务内部异常"),
    DEPLOY_DOING_NOW(600, "部署进行中,待稍后完成可再进行部署"),
    DEPLOY_NO_SETING(302,"部署未配置,无法执行"),
    COCO_NO_SETING(304,"覆盖率未配置,无法执行"),
    PLAN_NO_SETING(303,"测试计划未配置,无法执行"),

    // 7** 知音楼异常
    YACH_ERROR(700, "知音楼发送消息异常"),
    TOO_MANY_TEST_PLAN(900, "单次测试计划执行不能超过5个"),

    LOGIN_ERROR(1000, "用户登录异常"),

    // 5*** 测试报告异常相关
    REPORT_SEND_ERROR(5000, "测试报告发送异常"),

    // 3*** 提测异常相关
    SEND_TC_ERROR(3000, "发送提测知音楼消息异常"),

    // 4*** 提测异常相关
    OUT_SERVICE_ERROR(4000, "下游接口处理异常"),

    // 11** 参数异常
    ARGUMENTS_ERROR(1100, "参数校验失败不合法"),

    // 没有权限
    NOT_PERMISSIONS(9999, "您没有操作权限!");

    @Getter
    private int code;

    @Getter
    private String message;

    ResponseCode(int code, String message) {
        this.code = code;
        this.message = message;
    }
}

使用

     @GetMapping("/test")
    public MsgResponse test(@RequestParam Map<String,Object> params){
		//查询列表数据分页
        Query query = new Query(params);
        List<TestEntity> list = TestService.queryList(query);
        int total = TestService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());

        return MsgResponse.success(pageUtil);
    }
    
    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public Result test(@RequestBody TestEntity testEntity){
        TestService.save(testEntity);
        return Result.ok();
    }
    
    @PostMapping("/QRLogin")
    @ResponseBody
    public MsgResponse<UserAuthVO> login(@RequestBody final Token token) {
        MsgResponse<UserAuthVO> response = new MsgResponse<>();
        try {
            UserAuthVO vo = authService.login(token.getToken());
            response.setData(vo);
        } catch (Exception e) {
            log.error("用户登录异常", e);
            response.setCode(ResponseCode.LOGIN_ERROR.getCode());
            response.setMessage(ResponseCode.LOGIN_ERROR.getMessage());
        }
        return response;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cyril-zxy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值