• yml 字符串换行问题


    spring boot 推荐使用 yaml 格式语言(yml=yaml)来编写配置文件,从而取代 xml 以及 properties,yaml 语言具有像 json 一样简洁明了的特点,但同时具有能够处理复杂类型数据、文本注释的特点,非常适合作为配置文件使用。

    本文介绍一下在实际开发过程中关于 yaml 语言字符串换行的问题。
    yaml 语言在线编辑: https://www.bejson.com/validators/yaml_editor/

    yaml源代码:


    multiLineString1: |
    line1 hello world
    line2 yaml demo
    line3 welcome

    multiLineString2: >
    line1 hello world
    line2 yaml demo
    line3 welcome

    multiLineString3: "line1 hello world
    line2 yaml demo
    line3 welcome"

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    转义之后的代码:

    multiLineString1: 'line1 hello world line2 yaml demo line3 welcome ',

    multiLineString2: 'line1 hello world line2 yaml demo line3 welcome ',

    multiLineString3: 'line1 hello worldline2 yaml demoline3 welcome',
    1
    2
    3
    4
    5
    从结果可以看出,使用 | 会保留换行符,使用 > 没有换行符,但是两行字符串之间会有空格,这两种是绝大多数查阅到的 yaml 文件说明文档的写法,但是在 spring boot 配置文件中是不行的,因为这不再是一个完整的字符串,而是把一个字符串拆成了多段,spring boot 读取配置文件时会报错,因此需要使用第三种方式来将一个字符串拆成多行,解析之后仍然是一个完整的字符串。

    在 spring boot 中正确配置数据源的姿势:

    ## datasource
    spring:
    datasource:
    url: "jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf8&useJDBCCompliantTimezoneShift=true
    &useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=true&allowMultiQueries=true&autoReconnect=true"
    username: root
    password: sasa
    driver-class-name: com.mysql.jdbc.Drive

    1
    2
    3
    4
    5
    6
    7
    8
    9
    如果是使用 | 或者 > 来处理字符串换行则可能抛出以下异常:

    2018-10-10 22:16:00.362 ERROR 4620 --- [nio-8088-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
    ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: The connection property 'autoReconnect' only accepts values of the form: 'true', 'false', 'yes' or 'no'. The value 'true"' is not in this set.
    ### The error may exist in file [D:develop epositorygitspringBootDemodemo-dao argetclassesmapperusermapper.xml]
    ### The error may involve com.ljq.demo.springboot.dao.user.UserDao.queryList
    ### The error occurred while executing a query
    ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: The connection property 'autoReconnect' only accepts values of the form: 'true', 'false', 'yes' or 'no'. The value 'true"' is not in this set.] with root cause

    java.sql.SQLException: The connection property 'autoReconnect' only accepts values of the form: 'true', 'false', 'yes' or 'no'. The value 'true"' is not in this set.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    at com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.validateStringValues(ConnectionPropertiesImpl.java:314) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    at com.mysql.jdbc.ConnectionPropertiesImpl$BooleanConnectionProperty.initializeFrom(ConnectionPropertiesImpl.java:91) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    at com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.initializeFrom(ConnectionPropertiesImpl.java:216) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    at com.mysql.jdbc.ConnectionPropertiesImpl.initializeProperties(ConnectionPropertiesImpl.java:2538) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    at com.mysql.jdbc.ConnectionImpl.initializeDriverProperties(ConnectionImpl.java:3143) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:762) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    大致意思是 spring 在读取数据源 url 时并没有解析到完整的配置,因此需要使用上边正确的方式来配置。
    ————————————————
    版权声明:本文为CSDN博主「Flying9001」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/Mrqiang9001/article/details/83002988

  • 相关阅读:
    ADB常用命令
    HttpClient4.x 使用cookie保持会话
    HttpClient发起Http/Https请求工具类
    MakeFile 文件的使用
    SQL连接查询和嵌套查询详解
    Linux中进程控制块PCB-------task_struct结构体结构
    Mysql数据库--语句整理/提升/进阶/高级使用技巧
    进程池分析
    mmap和shm共享内存的区别和联系
    IO多路复用模型之epoll实现机制
  • 原文地址:https://www.cnblogs.com/whm-blog/p/12048305.html
Copyright © 2020-2023  润新知