@ControllerAdivice的三种用法

@ControllerAdivice的三种用法

第一种用法——自定义异常处理

第二种用法——处理全局异常

在这里插入图片描述

在启动类夫文件下新建文件HelloController

package cn.itxiaoliu;

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";
    }
}

再新建文件GlobalData

package cn.itxiaoliu;

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","xiaoliu");
        map.put("address","www.liuzuoping.com");
        return map;
    }
}

访问localhost:8080/hello

程序返回info:{address=www.liuzuoping.com, name=xiaoliu}

第三种用法——请求参数预处理

新建Book类与Author类

package cn.itxiaoliu;

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

package cn.itxiaoliu;

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

再新建文件BookController

package cn.itxiaoliu;

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

并且再GlobalData中加入以下代码

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

在postman中测试

在这里插入图片描述

程序返回

Book{name=‘三国演义’, price=99.0}
Author{name=‘罗贯中’, age=60}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值