JAVA 草稿

发布时间 2023-06-28 14:44:16作者: 之士咖啡

1. 项目启动报错:Failed to start bean 'documentationPluginsBootstrapper'

度娘解释:swagger 的匹配模式进行调整导致,导致默认的匹配默认在spring boot中不会使用,导致报错。
处理方式:(不建议降低spring boot 版本,调整太大了)

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

2. spring gateway路由出现503

是因为在Spring Cloud 2020版本以后,默认移除了对Netflix的依赖,其中就包括Ribbon,官方默认推荐使用Spring Cloud Loadbalancer正式替换Ribbon,并成为了Spring Cloud负载均衡器的唯一实现,只需要将含有这个过滤器的依赖进行导入就行了

<dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>

3. spring gateway路由出现404

配置如下,我期望路径包含/ebook/**时,能向service-book服务发请求,进入指定方法体。

spring:
  cloud:
    gateway:
      routes:
        - id: ebook
          uri: lb://service-ebook  #用百度做转发测试 http://www.baidu.com
          predicates: # 断言
            - Path=/ebook/**

实际情况报错:
image
报错原因:
gateway,获取到指定请求后返回请求的路径为http://127.0.0.1:8890/ebook/auth/code,但实际上路径应为/auth/code,没有/ebook
处理方式

spring:
  cloud:
    gateway:
      default-filters:
        - StripPrefix=1

规则:
StripPrefix=1 :/ebbok/auth/code -> /auth/code
StripPrefix=2 :/ebbok/auth/code -> /code
......

4. List 转为 Map

public void test4() {
        Student stu1 = new Student("张三", 23);
        Student stu2 = new Student("李四", 24);
        Student stu3 = new Student("王五", 25);
        Student stu4 = new Student("赵六", 23);
        Student stu5 = new Student("前七", 25);
 
        List<Student> strList = new ArrayList<>();
        Collections.addAll(strList, stu1, stu2, stu3, stu4,stu5);
        // 1.根据age作为key,name作为value转map(age相同时前面覆盖后面的数据)
        Map<Integer, String> collect = strList.stream().collect(Collectors.toMap(Student::getAge, Student::getName, (key1,key2) -> key1 ));
        for (Map.Entry<Integer, String> integerStudentEntry : collect.entrySet()) {
            System.out.println(integerStudentEntry.getKey() + ":" + String.valueOf(integerStudentEntry.getValue()));
        }
 
        // 2.根据age作为key,student对象作为value转map(age相同时前面覆盖后面的数据)
        Map<Integer, Student> collectStu = strList.stream().collect(Collectors.toMap(Student::getAge, Function.identity(), (key1, key2) -> key2));
        for (Map.Entry<Integer, Student> integerStudentEntry : collectStu.entrySet()) {
            System.out.println(integerStudentEntry.getKey() + "::" + String.valueOf(integerStudentEntry.getValue()));
        }
 
        // 3.根据age作为key,student对象作为value分组转map(age相同时前面覆盖后面的数据)
        Map<Integer, List<Student>> listMap = strList.stream().collect(Collectors.groupingBy(Student::getAge));
        for (Map.Entry<Integer, List<Student>> integerStudentEntry : listMap.entrySet()) {
            System.out.println(integerStudentEntry.getKey() + "::" + String.valueOf(integerStudentEntry.getValue()));
        }
    }