springboot-接口返回统一规范封装的方法

目录:

 

Constants     ---常量

package com.全局捕获.common;

public interface Constants {

    String CODE_200 = "200"; //成功
    String CODE_401 = "401";  // 权限不足
    String CODE_400 = "400";  // 参数错误
    String CODE_500 = "500"; // 系统错误
    String CODE_600 = "600"; // 其他业务异常

    String DICT_TYPE_ICON = "icon";

    String FILES_KEY = "FILES_FRONT_ALL";

}

 

Result    ----扔出前端响应的模版

package com.全局捕获.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 接口统一返回包装类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {

    private String code;
    private String msg;
    private Object data;

    public static Result success() {
        return new Result(Constants.CODE_200, "", null);
    }

    public static Result success(Object data) {
        return new Result(Constants.CODE_200, "", data);
    }

    public static Result error(String code, String msg) {
        return new Result(code, msg, null);
    }

    public static Result error() {
        return new Result(Constants.CODE_500, "系统错误", null);
    }

}

 

 

UserDTO

package com.全局捕获.Control.dto;

import lombok.Data;

/**
 * 接受前端请求的参数
 */
@Data
public class UserDTO {
    private String username;
}

 

TestControl

package com.全局捕获.Control;


import com.全局捕获.Control.dto.UserDTO;
import com.全局捕获.common.Constants;
import com.全局捕获.common.Result;
import com.全局捕获.exception.ServiceException;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class TestControl {

    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO) {
        String username = userDTO.getUsername();
        System.out.println(username);
        System.out.println(username.getClass().getName());
        if (username.equals("123")){
             --打印结果Result(code=400, msg=参数错误, data=null)  因为注解使用@RestController,返回实体类会自动映接口返回json
            System.out.println(Result.error(Constants.CODE_400, "参数错误"));   
           
            return Result.error(Constants.CODE_400, "参数错误");
        }
        if (username.equals("444")) {
            System.out.println(11111);
            throw new ServiceException(Constants.CODE_600, "用户名或密码错误");
        }
        return Result.success("正常请求");
    }
}

 

GlobalExceptionHandler     -----如果有逻辑调用 ServiceException 方法,那么这边会接收他对应的参数且去执行handle方法

package com.全局捕获.exception;


import com.全局捕获.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 如果抛出的的是ServiceException,则调用该方法
     * @param se 业务异常
     * @return Result
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public Result handle(ServiceException se){
        return Result.error(se.getCode(), se.getMessage());
    }

}

 

ServiceException

package com.全局捕获.exception;

import lombok.Getter;

/**
 * 自定义异常
 */
@Getter
public class ServiceException extends RuntimeException {
    private String code;

    public ServiceException(String code, String msg) {
        super(msg);
        this.code = code;
    }

}

 

图解:

posted @ 2022-06-09 11:56  凯宾斯基  阅读(1020)  评论(0编辑  收藏  举报