SpringBoot学习之@ControllerAdvice

@ControllerAdvice有三方面的功能:

1、全局异常处理

新建MyGlobalException类

package org.hx.springboot_uploadfile_demo17;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
//@RestControllerAdvice
public class MyGlobalException {
    @ExceptionHandler(Exception.class)
    public ModelAndView customException(Exception e)
    {
        ModelAndView modelAndView = new ModelAndView("error");
        modelAndView.addObject("err",e.getMessage());
        return  modelAndView;
    }
}

新建一个error.html文件

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>hello hx-thymeleaf</h1>
    <div th:text="${err}"></div>
</body>
</html>

当上传文件超过1MB时,返回错误信息页面,页面输出详细错误信息

2、全局数据绑定

新建MyGlobalData类

package org.hx.springboot_controlleradvice_demo18;

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

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

@ControllerAdvice
public class MyGlobalData {
    @ModelAttribute
    public Map<String,String>  mydata(){
        Map<String ,String> info = new HashMap<>();
        info.put("username","npu");
        info.put("address","hx");
        return info;
    }
}

新建HelloController类

package org.hx.springboot_controlleradvice_demo18;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

@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("map");
        Set<String> keySet = info.keySet();
        for (String s : keySet) {
            System.out.println(s+"======"+info.get(s));
        }
    }
}

3、全局数据预处理

新建Author类和Book类

package org.hx.springboot_controlleradvice_demo18.model;

public class Author {
    private String name;
    private Integer age;

    @Override
    public String toString() {
        return "Author{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

package org.hx.springboot_controlleradvice_demo18.model;

public class Book {
    private String name;
    private Double price;

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }
}

在MyGlobalData中补充

 @InitBinder("b")
 //在匹配Controller的请求执行映射的方法之前执行; 同时 @InitBinder标注的方法执行是多次的,一次请求来就执行一次
    public void b(WebDataBinder binder){
        binder.setFieldDefaultPrefix("b.");
    }
    @InitBinder("a")
    //WebDataBinder相当于一个前后台的转换器:前台通过SpringMVC传递过来的属性值会自动对应
    // 到对象中的属性并封装成javaBean,但是只能是基本数据类型(int,String等)
    public void a(WebDataBinder binder)
    {
        binder.setFieldDefaultPrefix("a.");
    }

新建BookController类

package org.hx.springboot_controlleradvice_demo18;

import org.hx.springboot_controlleradvice_demo18.model.Author;
import org.hx.springboot_controlleradvice_demo18.model.Book;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@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);

    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值