1. 程式人生 > >Spring Boot之application.properites的failed to convert java.lang.String to java.lang.Integer問題解決

Spring Boot之application.properites的failed to convert java.lang.String to java.lang.Integer問題解決

環境描述

Spring Boot 2.0.4.RELEASE

問題描述

新建立了一個Spring Boot的Web應用,在pom.xml中的profile中配置了web server的埠,並在application.properties中配置了對應的port欄位對映。
但是,在啟動過程中,卻出現瞭如下錯誤資訊:


***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'server
.port' to java.lang.Integer: Property: server.port Value: @app.server.port@ Origin: class path resource [application.properties]:4:13 Reason: failed to convert java.lang.String to java.lang.Integer Action: Update your application's configuration

pom.xml中對應的配置如下:

     <profile
>
<id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <app.name>robotcode</app.name> <app.log.level>debug</app.log.level
>
<app.server.port>1090</app.server.port> </properties> </profile>

應用中的application.properties的配置如下:

spring.application.name=Robot Code Application
server.port[email protected].server.port@

問題分析

針對問題的錯誤資訊,嘗試進行猜測:
1. 埠的欄位名稱配置錯誤, 導致真實的埠沒有出現。
2. 埠的不能這麼配置,方式問題
3. app.server.port這個欄位值需要自行型別轉換嗎?

猜測1, 經過檢查,問題不存在。 猜測2: 應該沒有問題。 猜測3: Spring Boot如果配置一個埠都需要自行轉換,那這個框架設計的就臺lower了。

那問題是不是欄位未被替換,或者替換的值不對呢?
大概率應該是並未被替換,所以,在轉換過程中,出現型別錯誤資訊。

mvn package

自行打包一次,進行檢查,果然如此。

問題解決

在pom.xml中配置相應的過濾替換機制:

    <plugins>
        ............
         <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin> -->
        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

總結

重新執行一下,問題被完美解決。哈哈,一個很有意思的問題,能夠加深大家對於PropertyPlaceLoader等相關方法的理解。

相關推薦

Spring Bootapplication.properites的failed to convert java.lang.String to java.lang.Integer問題解決

環境描述 Spring Boot 2.0.4.RELEASE 問題描述 新建立了一個Spring Boot的Web應用,在pom.xml中的profile中配置了web server的埠,並在application.properties中配置了對應的

Spring-Bootapplication.properties檔案與application.yml檔案比較

      預設建立spring-boot專案後,會在resources目錄下生成一個空的application.properties配置檔案,springboot啟動時載入該配置檔案。    &nbs

Spring BootNo session repository could be auto-configured, check your configuration問題解決

1.  環境介紹     JDK 1.8  Spring-Boot 1.5.1.RELEASE, STS IDE2.  問題的提出     建立了一個非常簡約的Spring Boot Web Application,其中使用了Spring-Session,具體的maven依賴

spring Failed to convert property value of type 'java.lang.String' to required type 'int' for proper

這種情況,看起來好像是 spring 的org.springframework.beans.factory.config.PropertyPlaceholderConfigurer類在讀取資訊的時候出問題了,把 String 型別的值當成了 int 型別的了實際上這並不是這麼

Spring Boot annotation註解

頁面 osi 數據 加載 依賴註入 bsp 類型 數據庫 聲明 一:基於類的註解:(1)初始裝載@SpringBootApplication spring-boot程序入口標誌類@Configuration

spring boot 熱部署(三)

classpath spring dev .com odin 部署 logs com pat 熱部署:當發現程序修改時自動啟動應用程序。 spring boot使用的是spring-boot-devtools是一個為開發者服務的一個模塊。其原理用了classL

Spring-boot Swagger2(打造不一樣的api)

plugin itl pid 研究 ssa any cati plugins ast 一、Swagger2是什麽? Swagger 是一款RESTFUL接口的文檔在線自動生成+功能測試功能軟件。 Swagger 是一個規範和完整的框架,用於生成、描述、調用和可視化 RE

Spring-bootjQuery File Upload後臺配置方法

文件上傳 query jquer highlight origin exc exist int alt 文件上傳在Spring-boot中本身配置起來非常簡單,但是有個多文件傳遞和單個傳遞的問題。 兩者配置是略有不同的,而且還有一些讓我這個技術小白很容易踩坑的地方。 重

從零一起學Spring BootLayIM項目長成記(一) 初見 Spring Boot

部分 基礎 依賴 com stat boot.s 情況下 比較 tar 項目背景   之前寫過LayIM的.NET版後端實現,後來又寫過一版Java的。當時用的是servlet,websocket和jdbc。雖然時間過去很久了,但是仍有些同學在關註。偶然間我聽說了Sprin

spring-bootapplication.properties的各種配置

ecif creat ati .sh drive fig nag ide cte ###########################################################datasource connect mysql#############

從零一起學Spring BootLayIM項目長成記(六)單聊群聊的實現

href 宣傳 net 放棄 blog 群聊 項目 get 實現 文章傳送門:   https://my.oschina.net/panzi1/blog/1577007 並沒有放棄博客園,只是 t-io 在 oschina發展。用了人家的框架,也得幫人家做做宣傳是吧~~從零

Spring Boot HelloWorld詳解

www repos lookup dex lns jar nap put epo SpringBoot介紹~<暫時假裝有> 配置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht

Spring Bootapplication.properties和application.yml文件

undefined fan href 目錄 分享圖片 boot 分享 ref 列表 application.properties和application.yml文件可以放在一下四個位置: 外置,在相對於應用程序運行目錄的/congfig子目錄裏。 外置,在應用程序運行的目

Spring Boot 啟動報錯 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 37

work 結果 frame getc director inf 中文 getclass post 使用命令 java -jar springBoot.jar 啟動項目,結果報錯如下: Exception in thread "main" java.lang.String

Spring BootHello World

lns mode mls compile ase closed amp 創建 AC 通過Spring Boot【1.5.11.RELEASE】 官方文檔進行學習,並記錄一點一滴的成長。 Spring Boot 簡介對於Spring可以輕松地創建獨立的、生產級的Spri

關於Spring boot中讀取屬性配置文件出現中文亂碼的問題的解決(針對application.properties)

HA inf encoding 屬性 文件中 ide for 出現 spring 兩種方法: 方法一:在配置文件中設置中文編碼: banner.charset=utf-8server.tomcat.uri-encoding=UTF-8spring.http.encoding

spring boot入門配置(一)

麻煩 config src 符號 pos files 分享圖片 PE strong yml、properties配置文件   yml相比properties配置文件,yml可以省略不必要的前綴,並且看起來更加的有層次感。推薦使用yml文件。    @Value   根據

spring boot項目升級到2.0.1,提示java.lang.ClassNotFoundException: org.apache.log4j.Logger錯誤

ava lan 簡單 src AS dex ack pen pid 首先肯定是版本出現了一點兼容問題 所以我就進入項目,查看依賴樹,cd到項目目錄下,mvn dependency:tree 我看到我的項目原先是1.5.9和現在2.0.1的區別在於org.slf4j的依賴不全

Spring BootWebSocket

package 說明 clas return pre tell ebs 異常 override 一、項目說明 1、項目地址:https://github.com/hqzmss/test01-springboot-websocket.git 2、IDE:IntelliJ ID