码迷,mamicode.com
首页 > 编程语言 > 详细

spring boot 全局异常处理

时间:2020-04-13 12:15:26      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:ast   empty   form   bsp   使用   comm   code   ali   als   

0.使用 @ ExceptionHandler 注解(Controller层面上)

   严格来说不是全局,是单类异常处理,进行异常处理的方法必须与出错的方法在同一个Controller里面。

package com.lvjing.controller;

import com.alibaba.fastjson.JSON;
import com.lvjing.bean.ResponseBeanWithCount;
import com.lvjing.common.ResponseBean;
import com.lvjing.dao.CommuteLogMapper;
import com.lvjing.domain.CommuteLog;
import lombok.extern.java.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:ZY
 * @Descirption:
 * @Date: Created by ZY on 2018/1/3.
 * @Modified By:
 */
@Log
@RestController
@RequestMapping(value="/commute")
public class CommuteLogController {

    private CommuteLogMapper commuteLogMapper;
    @Autowired
    private void setCommuteLogMapper(CommuteLogMapper commuteLogMapper){
        this.commuteLogMapper = commuteLogMapper;
    }

    /**
     * @Description: 查询进出电子围栏
     * @author: ZY
     * @param: [paraMap]
     * @return: com.lvjing.common.ResponseBean
     * @Date: 2019/1/3
     */
    @RequestMapping(value = "/getCommuteLogList", method = RequestMethod.POST)
    public ResponseBeanWithCount getDeviceWarningLogList(@RequestBody Map<String, Object> paraMap) throws Exception {
        log.info("查询进出电子围栏人员列表,参数="+ JSON.toJSONString(paraMap));
        if ("0".equalsIgnoreCase(paraMap.get("id").toString())){
            throw new Exception("ID==0");
        }

        CommuteLog commuteLog = JSON.parseObject(JSON.toJSONString(paraMap), CommuteLog.class);
        if (paraMap.isEmpty()) {
            return new ResponseBeanWithCount(-1, "查询条件不能为空", 0, null);
        }
        int count = commuteLogMapper.queryCount(commuteLog);
        List<CommuteLog> deviceWarningLogs = commuteLogMapper.query(commuteLog);
        log.info("查询进出电子围栏人员列表,返回值="+ JSON.toJSONString(deviceWarningLogs));
        return new ResponseBeanWithCount(0, "查询成功!", count, deviceWarningLogs);
    }
    @ExceptionHandler({Exception.class})
    private ResponseBeanWithCount handleException(Exception e){
        e.printStackTrace();
        log.info(e.getMessage());
        return new ResponseBeanWithCount(-1, "查询失败", 0, null);
    }

}

技术图片

 

 

 

1.

spring boot 全局异常处理

标签:ast   empty   form   bsp   使用   comm   code   ali   als   

原文地址:https://www.cnblogs.com/zhuangyao/p/12690216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!