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