ControllerAdvice的使用

controlleradvice类
1. 使用该注解 , 该注解标识的类会获取并修改controller中爆出的相应异常.

package com.imooc.demo.exception;

import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ControllerAdvice
public class ControllerExceptionHandler {

    @ExceptionHandler(value = MyException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Map<String,Object> handlerUserNotExistException(MyException m) {
        Map<String ,Object> map = new HashMap<>();
        map.put("id",m.getId());
        map.put("message",m.getMessage());
        return map;
    }
// 该方法无效 , 待修改
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public Map<String,Object> handlerValidExistException(BindingResult result) {
        Map<String ,Object> map = new HashMap<>();
        if (result != null) {
            if (result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                for (int i = 0; i < errors.size(); i++) {
                    map.put(Integer.toString(i),errors.get(i).getObjectName());
                }
            }
        } else {
            map.put("field","result is null");
        }
        return map;
    }
}
  1. controller异常类
package com.imooc.demo;

import com.imooc.demo.exception.MyException;
import com.imooc.demo.security.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@SpringBootApplication
@RestController
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class,args);
    }

    @PostMapping("")
    public String hello() {

        // return "success";
        throw new MyException("12","haha");
        // return "hello";
    }
}
  1. 自定义异常类
package com.imooc.demo.exception;

public class MyException extends RuntimeException{

    private String id;


    public MyException(String id,String message) {
        super(message);
        this.id = id;
    }
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
  1. 请求url
http://localhost:8080/
  1. 报错信息
{
"id": "12",
"message": "haha"
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值