结果工具类使用Result、MsgResponse

结果工具类使用

    • 1、Result 结果工具类
    • 2、MsgResponse结果工具类

目的:结果展示规范化

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;
    }

你可能感兴趣的:(java,jvm,spring)