Spring学习记录-异常处理

修改了controller

package com.example.springio;

import com.example.springio.exeption.myException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicLong;
@RestController
public class GreetingController {
    private final String temple = "hello %s";
    private final AtomicLong conter = new AtomicLong();

    @GetMapping("/Greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name){
        return new Greeting(conter.incrementAndGet(),String.format(temple,name));
    }
    @GetMapping("/login")
    public Greeting login(@RequestParam(value = "name", defaultValue = "World") String name){
        if(name.equals("a")){
           throw new myException("用户名错误","401");
        }else{
            int a = 5/0;
        }

        return new Greeting(conter.incrementAndGet(),String.format(temple,name));
    }

}

此时这里触发了代码异常
执行了exception
在这里插入图片描述

package com.example.springio;

import com.example.springio.exeption.myException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@ControllerAdvice
public class exception {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object exception(Exception e){
        Map<String,Object> map = new HashMap<>();
        map.put("success",false);
        map.put("code","ERR000");
        map.put("messege",e.getMessage());
        return map;
    }


    @ExceptionHandler(myException.class)
    @ResponseBody
    public Object myexception(myException e){
        Map<String,Object> map = new HashMap<>();
        map.put("success",false);
        map.put("code","ERR001");
        map.put("messege",e.getMessage());
        log.debug(getlog(e));
        return map;
    }
    public String getlog(Exception e){
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        return sw.toString();

    }
}

然后尝试抛出自定义异常
在这里插入图片描述
此时调用的为

  if(name.equals("a")){
           throw new myException("用户名错误","401");
    @ExceptionHandler(myException.class)
    @ResponseBody
    public Object myexception(myException e){
        Map<String,Object> map = new HashMap<>();
        map.put("success",false);
        map.put("code","ERR001");
        map.put("messege",e.getMessage());
        log.debug(getlog(e));
        return map;
    }

为了把堆栈信息打印到log
引入了@Slf4j
这样可以调用

 log.debug()

这里封装一个方法把exception转化成string


    public String getlog(Exception e){
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        return sw.toString();

    }

最后附上myExeption,注意继承了RuntimeException


import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class myException extends RuntimeException{
    private   String code;

    public myException(String message, String code) {
        super(message);
        this.code = code;
    }
    public myException(String message, Throwable cause, String code) {
        super(message, cause);
        this.code = code;
    }
    


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值