全局数据预处理

Exisi 2022-06-28 14:35:05
Categories: Tags:

 

 

public class Book{
   
private String name;
   
private Long price;
   
//getter/setter

}


public class Author{
   
private String name;
   
private Integer age;
   
//getter/setter

}

 

@PostMapping("/book")

public void addBook(Book book, Author author){
    System.out.println(book);
    System.out.println(author);
}

 

示例

@Controller

public class bookController{

//给接口中的变量取别名

@PostMapping("/book")

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

}

 

@ControllerAdvice

public class MyGlobalExceptionHandler{

// @ControllerAdvice 标记的类中添加@InitBinder

@InitBinder("b")

public void b(WebDataBinder binder){

    binder.setFieldDefaultPrefix("b."); //绑定前缀

}

 

@InitBinder("a")

public void a(WebDataBinder binder){

    binder.setFieldDefaultPrefix("a."); //绑定前缀

}

}

 

 

 

 

来自 <https://www.cnblogs.com/lenve/p/10748453.html>