新版Spring Boot(6)- Spring Boot 整合 Web 开发(2)

本文详细介绍了如何在Spring Boot应用中使用@ControllerAdvice实现全局异常处理,包括异常页面定义和自定义异常处理。同时涵盖全局数据绑定和请求参数预处理的技术。通过实例展示了如何创建自定义的错误属性和视图解析器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 @ControllerAdvice

  • 全局异常处理
  • 全局数据绑定
  • 全局数据预处理

1.1 @ControllerAdvice 全局异常处理

  • 文件上传案例
    在这里插入图片描述
package com.tzb.exception;


import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description 全局异常处理
 * @Author tzb
 * @Date 2021/8/15 10:54
 * @Version 1.0
 **/

@RestControllerAdvice
public class MyGlobalException {

    @ExceptionHandler(Exception.class)
    public String customException(Exception e) {
        return e.getMessage();
    }

}

在这里插入图片描述

1.2 全局数据绑定

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;

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

/**
 * @Description 全局数据绑定
 * @Author tzb
 * @Date 2021/8/15 11:08
 * @Version 1.0
 **/
@ControllerAdvice
public class MyGlobalData {

    @ModelAttribute(name = "info")
    public Map<String,String> mydata(){
        Map<String,String> info = new HashMap<>();
        info.put("name", "Mike");
        info.put("address","ShangHai");
        return info;
    }
}

@RestController
public class HelloController {

    @GetMapping("/hello")
    public void hello(Model model ){
        Map<String, Object> asMap = model.asMap();
        Map<String, String> info = (Map<String, String>)asMap.get("info");
        System.out.println(info);

    }
}

在这里插入图片描述

1.3 请求参数预处理

在这里插入图片描述
在这里插入图片描述

@ControllerAdvice
public class MyGlobalData {

    @ModelAttribute(name = "info")
    public Map<String,String> mydata(){
        Map<String,String> info = new HashMap<>();
        info.put("name", "Mike");
        info.put("address","ShangHai");
        return info;
    }

    @InitBinder("b")
    public void b(WebDataBinder binder){
        binder.setFieldDefaultPrefix("b.");
    }

    @InitBinder("a")
    public void a(WebDataBinder binder){
        binder.setFieldDefaultPrefix("a.");
    }
}

@RestController
public class BookController {

    @PostMapping("/book")
    public void addBook(@ModelAttribute("b") Book book, @ModelAttribute("a") Author author) {
        System.out.println("book:" + book);
        System.out.println("author:" + author);
    }
}

在这里插入图片描述
在这里插入图片描述

2 异常页面定义

2.1 直接根据错误码定义页面

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.2 异常处理原理

在这里插入图片描述

2.3 自定义异常

2.3.1 自定义数据

package com.tzb.exception;

import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

/**
 * @Description TODO
 * @Author tzb
 * @Date 2021/8/15 16:53
 * @Version 1.0
 **/
@Component
public class MyErrorAttribute extends DefaultErrorAttributes {

    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {

        Map<String, Object> map = super.getErrorAttributes(webRequest, options);

        if ((Integer) map.get("status") == 404) {
            map.put("message", "页面不存在");
        }

        return map;
    }
}

在这里插入图片描述

2.3.2 自定义视图

package com.tzb.exception;

import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.DefaultErrorViewResolver;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author tzb
 * @Date 2021/8/15 16:59
 * @Version 1.0
 **/
@Component
public class MyErrorViewResolver extends DefaultErrorViewResolver {
    public MyErrorViewResolver(ApplicationContext applicationContext, ResourceProperties resourceProperties) {
        super(applicationContext, resourceProperties);
    }

    @Override
    public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
        Map<String, Object> map = new HashMap<>();
        map.putAll(model);
        if((Integer)model.get("status") ==500){
            map.put("message", "服务器内部错误");
        }
        return new ModelAndView("myerror/999", map);
    }
}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值