SpringBoot使用ControllerAdvice

@ControllerAdvice

1、处理全局异常

2、预设全局数据

3、请求参数预处理

1 使用upload案例 设置异常

  • application.yml
spring:
  servlet:
    multipart:
      max-file-size: 1KB
  • MyCustomException
package com.thciwei.upload;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@ControllerAdvice
public class MyCustomException {
    
    
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public void myexception(MaxUploadSizeExceededException e, HttpServletResponse resp) throws IOException {
    
    
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.write("上传文件大小不能超出限制!");
        out.flush();
        out.close();

    }
}

注意设置 text/html;charset=utf-8,使resp视图不会乱码

上传文件时,超过1KB 显示“上传文件大小不能超出限制!”

2 预设全局数据

package com.thciwei.demo;

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 GlobalData {
    
    
    @ModelAttribute(value = "info")
    public Map<String, Object> mydata() {
    
    
        Map<String, Object> map = new HashMap<>();
        map.put("name", "java");
        map.put("address", "www.thciwei.com");
        return map;
    }

}
package com.thciwei.demo;

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 String hello(Model model) {
    
    
        Map<String, Object> map = model.asMap();
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
    
    
            System.out.println(key + ":" + map.get(key));
        }
        return "hello";
    }


}

打印内容 info:{address=www.thciwei.com, name=java}

3 请求参数预处理

  • Book
package com.thciwei.demo;
@Data
public class Book {
    
    
    private String name;
    private Double price;
    @Override
    public String toString() {
    
    
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
  • Author
package com.thciwei.demo;
@Data
public class Author {
    
    
    private String name;
    private Integer age;
    @Override
    public String toString() {
    
    
        return "Author{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
  • BookController
package com.thciwei.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BookController {
    
    
    @PostMapping("/book")
    public  void addBook(Book book,Author author){
    
    
        System.out.println(book);
        System.out.println(author);
    }
}

使用 postman发送数据

后台打印

Book{name='三国演义,罗贯中', price=99.0}

Author{name='三国演义,罗贯中', age=60}

name合并了,这是很差的接口设计,如果真的出现这种问题,可以通过我们的@ControllerAdvice解决

各给他们一个value

package com.thciwei.demo;

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);
        System.out.println(author);
    }
}

加前缀

package com.thciwei.demo;

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

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

@ControllerAdvice
public class GlobalData {
    
    
    @ModelAttribute(value = "info")
    public Map<String, Object> mydata() {
    
    
        Map<String, Object> map = new HashMap<>();
        map.put("name", "java");
        map.put("address", "www.thciwei.com");
        return map;
    }

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

}

猜你喜欢

转载自blog.csdn.net/thciwei/article/details/121915583
今日推荐