springboot 统一返回数据格式和统一异常处理

BaseResult.java:

package com.example.demo.bean.dto;

/**
 * @author 
 * @description TODO
 * @date 2019/4/22
 */
public class BaseResult {

    private int code;
    private String message;
    private T data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public BaseResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
}

 

ResultUtil.java

package com.example.demo.utils;

import com.example.demo.bean.dto.BaseResult;

/**
 * @author 
 * @description TODO
 * @date 2019/4/23
 */
public class ResultUtil {

    public static BaseResult success(){
        return new BaseResult(0, "success", null);
    }

    public static BaseResult success(Object data){
        return new BaseResult(0, "success", data);
    }

    public static BaseResult success(int code, String message){
        return new BaseResult(code, message, null);
    }

    public static BaseResult success(int code, String message, Object data){
        return new BaseResult(code, message, data);
    }

    public static BaseResult fail(String message){
        return new BaseResult(-1, message, null);
    }

    public static BaseResult fail(int code, String message){
        return new BaseResult(code, message, null);
    }

}

 

BusinessException.java:
package com.example.demo.exception;

/**
 * @author 
 * @description TODO
 * @date 2019/4/23
 */
public class BusinessException extends RuntimeException {

    private int code;

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

    public int getCode() {
        return code;
    }
}

 

ExceptionHandle.java
package com.example.demo.advice;

import com.example.demo.bean.dto.BaseResult;
import com.example.demo.exception.BusinessException;
import com.example.demo.utils.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 
 * @description TODO
 * @date 2019/4/23
 */
@ControllerAdvice
public class ExceptionHandle {


    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public BaseResult errorHandler(BusinessException ex) throws Exception{
        return ResultUtil.fail(ex.getCode(), ex.getMessage());
    }


    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public BaseResult errorHandler(Exception ex) {
        return ResultUtil.fail(ex.getMessage());
    }

}

TestController.java:

package com.example.demo.controller;

import com.example.demo.bean.User;
import com.example.demo.bean.dto.BaseResult;
import com.example.demo.service.TestService;
import com.example.demo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author yuyanwu
 * @description TODO
 * @date 2019/4/22
 */
@RestController
public class TestController {

    @Autowired
    private TestService testService;

    @PostMapping("/test")
    public BaseResult test(@RequestBody User user){
        float i = 1/0;
        return ResultUtil.success("aaa");
    }

    @GetMapping("/test1")
    public BaseResult test1(User user){
        testService.test();
        return ResultUtil.success(-1,"地模压");
    }

    @GetMapping("/test2/{indexCode}")
    public BaseResult test2(@PathVariable String indexCode){
        return ResultUtil.success(0,"地模压");
    }

}

 

你可能感兴趣的:(springboot 统一返回数据格式和统一异常处理)