Java-学习日记(函数式编程与@ControllerAdvice)
2021-03-26 09:26
标签:str runnable list org tor java 添加 optional tps 函数可以作为变量、参数、返回值和数据类型 相关内容有@FunctionalInterface(Runnable、Comparator内部添加了),Lambda和函数式接口 感觉上面这句话就说的很有道理,下面针对普通函数,lambda和具体内部实现来说下 从上面内部类和下面函数式接口可以看出,Lambda其实就是在函数式接口上进一步简化了代码 之前写的四个函数式接口: https://www.cnblogs.com/meditation5201314/p/13693089.html SpringMvc:@ControllerAdvice具备下面3个功能: 前面2个好理解,最后一个数据预处理在传递多个参数的时候,如果遇到变量名相同,后台就无法区分,所以需要配置进行区分(之前好像都没遇到过,记录一下) 给出两个相同类,Controller层里面就要进行区分(postman中就要使用u.userName,p.userName来进行区分,否则什么都不写的话原来默认就是全部覆盖.) Java-学习日记(函数式编程与@ControllerAdvice) 标签:str runnable list org tor java 添加 optional tps 原文地址:https://www.cnblogs.com/meditation5201314/p/13714417.html函数式编程:
普通函数
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("do something");
}
};
Lambda
Runnable runnable = () -> System.out.println("do Something");
Optional
内部函数式接口
Optional
package lambda;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
/**
* @ProjectName: Method
* @Package: lambda
* @Description:
* @Author: huyuqiao
* @CreateDate: 2020/9/22 9:17
*/
public class Method {
private static void oldFunction(){
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("do something");
}
};
}
private static void newFunction(){
Runnable runnable = () -> System.out.println("do Something");
}
private static void optionalFunction(){
Optional
@ControllerAdvice
@PostMapping("/book")
public void addBook(@ModelAttribute("u") User user, @ModelAttribute("p")Person person, Model model){
System.out.println(user);
System.out.println(person);
Map
package huyuqiao.exception;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;
/**
* @ProjectName: GloabalExceptionHandler
* @Package: huyuqiao.exception
* @Description:
* @Author: huyuqiao
* @CreateDate: 2020/9/22 19:01
*/
@ControllerAdvice
public class GloabalExceptionHandler {
/*全局异常处理---可以改成NullPointerException异常处理*/
@ExceptionHandler(Exception.class)
public ModelAndView customException(Exception e) {
ModelAndView mv = new ModelAndView();
mv.addObject("message", e.getMessage());
mv.setViewName("myerror");
return mv;
}
/*全局数据绑定*/
@ModelAttribute(name = "md")
public Map
文章标题:Java-学习日记(函数式编程与@ControllerAdvice)
文章链接:http://soscw.com/index.php/essay/68107.html