springBoot之自定义异常,返回对象

package com.huangliang.test.core;

public class MyException extends RuntimeException {
    private String errorCode;
    private String msg;

    public MyException(String message) {
        super(message);
    }

    public MyException(String errorCode, String msg) {
        this.errorCode = errorCode;
        this.msg = msg;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

 

 

package com.huangliang.test.core;

public class MyResponse<T> {
    private String code;
    private String msg;
    private String error;
    private T data;

    public MyResponse(T data) {
        this.code = "200";
        this.msg = "获取成功";
        this.error = "";
        this.data = data;
    }

    public MyResponse(String code, String msg, String error, T data) {
        this.code = code;
        this.msg = msg;
        this.error = error;
        this.data = data;
    }

    public String getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public T getData() {
        return data;
    }

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


}

 

 

package com.huangliang.test.modules.user.controller;

import com.huangliang.test.core.MyException;
import com.huangliang.test.core.MyResponse;
import com.huangliang.test.modules.user.domain.User;
import com.huangliang.test.modules.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;


    @RequestMapping(name = "/user/list", method = RequestMethod.GET)
    @ResponseBody
    public MyResponse getUserList() {
        List<User> list = userService.getUserList();
        if (list == null) {
            throw new MyException("获取对象为空");
        }
        return new MyResponse(list);
    }
}

 

 

 

返回数据:

{
    "code": "200",
    "msg": "获取成功",
    "error": "",
    "data": [
        {
            "id": 1,
            "name": "sam",
            "phone": "15928005689",
            "age": 22
        },
        {
            "id": 2,
            "name": "summer",
            "phone": "18302895614",
            "age": 25
        },
        {
            "id": 3,
            "name": "wang",
            "phone": "18756894422",
            "age": 24
        }
    ]
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值