首页 > 其他分享 >spring boot——请求与参数校验——重要概念——异常处理——@ControllerAdvice注解——华章

spring boot——请求与参数校验——重要概念——异常处理——@ControllerAdvice注解——华章

时间:2023-01-22 22:36:00浏览次数:58  
标签:code ControllerAdvice spring MyException boot import msg org public

 

 

 

 

 

 

 

 

 

 

 

 

package org.example.Exception;

public class MyException extends RuntimeException
{
    private int code;
    private String msg;

    public MyException(String msg){
        super(msg);
        this.code=500;
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

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

 

 

 

 

 

 

 

 

 

package org.example.controller.GlobalExceptionHandler;

import org.example.Exception.MyException;
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.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class GlobalExceptionHandlerController
{
    /**
     * 返回页面
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public ModelAndView handle(RuntimeException e){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("errorPage");
        modelAndView.addObject("code", 500);
        modelAndView.addObject("msg", e.getMessage());
        return modelAndView;
    }

    /**
     * 返回JSON
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(MyException.class)
    public Map<String,Object> handleMyException(MyException e){
        Map<String,Object> map = new HashMap<>();
        map.put("code",e.getCode());
        map.put("message",e.getMsg());
        return map;
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package org.example.controller.GlobalExceptionHandler;

import org.example.Exception.MyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;

@RestController
public class HandleExceptionController
{
//    @RequestMapping("/f123")
//    public String index()
//    {
//
//        return "index";
//    }
//
//    @RequestMapping("/db")
//    public void db() throws SQLException
//    {
//
//        throw new SQLException("数据库异常");
//    }
//
//    @RequestMapping("/my")
//    public void my() throws MyException
//    {
//
//        throw new MyException("自定义异常");
//    }
//
//    @RequestMapping("/no")
//    public void no() throws Exception
//    {
//
//        throw new Exception("未知异常");
//    }
//
//
//
    @RequestMapping("/index")
    public String index(){
        int n = 1/0;
        return "SUCCESS";
    }

    @RequestMapping("/test")
    public String testException(){
        throw new MyException("sorry,it is too bad");
    }


}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:code,ControllerAdvice,spring,MyException,boot,import,msg,org,public
From: https://www.cnblogs.com/xiaobaibailongma/p/17064748.html

相关文章