zoukankan      html  css  js  c++  java
  • spring boot升级到2.x的坑

    升级到spring boot 2.x后,发现了好多坑,现记录下来。

    1、pom文件依赖的变化

    1.x中,依赖是这样的:

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

    2.x,依赖是这样的:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

    猜测是将eureka移到netflix包中了。目前发现依赖变化的有:eureka、feign、hystrix

    2、Repository取消了findOne(id)方法

    1.x中,有findOne(id)方法

    User user = userRepository.findOne(Long id)

    2.x中,取消了findOne(id)方法,可以使用getOne(id)或者findById(id)

    User user = userRepository.getOne(id);
    User user = userRepository.findById(id).get();

    3、使用eureka注册中心时,eureka客户端注册不了,报错如下:

    com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

    原因是升级后,security默认启用了csrf检验,要在eurekaServer端配置security的csrf检验为false。需要增加的配置类如下:

    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable();
            super.configure(http);
        }
    
    }

    4、springboot 2.1以上的版本,会出现启动时不打印日志的问题(一直卡着,但是服务启动成功),控制台打印如下:

    问题原因:springboot高版本排除了log4j依赖,需要手动添加 

    解决办法:在pom文件中增加依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

    5、配置context-path:

    1.x中配置:

    server:
      port: 8091
      context-path: /order

    2.x中配置:

    server:
      port: 8091
      servlet:
        context-path: /order
  • 相关阅读:
    个人浏览器安装的插件
    angularjs 1 Failed to read the 'selectionStart' property from 'HTMLInputElement':
    git 配置用户名和邮箱
    修改Atom 隐藏.gitignore忽略的文件/文件夹的配置
    Ubuntu 安装php_intl 扩展
    yii2 ./yii command : No such file or directory
    vagrant yii2 Exception 'yiidbException' with message 'SQLSTATE[HY000] [2002]
    [转载]Ubuntu 14.04设置固定ip
    [转载]删除所有的.svn文件夹
    Centos7.2正常启动关闭CDH5.16.1
  • 原文地址:https://www.cnblogs.com/xuwenjin/p/10733965.html
Copyright © 2011-2022 走看看