Spring MVC的@ResponseStatus注解

一 视图

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




异常处理示例



@ResponseStatus异常处理


二 自定义异常类

package org.fkjava.exception;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(reason="查询数据失败")
public class BookException extends RuntimeException {
}

三 控制器

package org.fkit.controller;


import org.fkjava.exception.BookException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class BookController{
    
    @GetMapping("/find")
    public String find() throws Exception{
        try {
            int i = 5/0;
            return "success";
        } catch (Exception e) {
            throw new BookException();
        }
    }
}

四 配置文件



        
    
    
    
    
     
    
    
    
     
    

五 测试报告

Spring MVC的@ResponseStatus注解_第1张图片

Spring MVC的@ResponseStatus注解_第2张图片

你可能感兴趣的:(Spring,MVC)