Springboot上傳文件時提示405問題及排坑過程
Springboot上傳文件時提示405
問題描述:上傳文件時請求不通,狀態(tài)碼返回405,如下圖:
問題分析:405 Method Not Allowed,請求行中指定的請求方法不能被用于請求相應(yīng)的資源。該響應(yīng)必須返回一個Allow 頭信息用以表示出當前資源能夠接受的請求方法的列表。簡單說就是請求方法不支持,這樣就找到了一個解決方向。(以下分析了3種返回該錯誤的情況)
解決方案1
看下接口是否支持請求的方式,文件上傳使用的POST方法,看下接口是否支持。后臺日志如下:
解決方案2
發(fā)現(xiàn)接口確實支持POST請求,那么問題就不是這么明顯了。因為該接口是用于文件上傳,所以問題應(yīng)該是在這里。由于Springboot默認的文件上傳大小為1MB,自己再看發(fā)現(xiàn)文件大小超過了限制(上傳的7.13MB),后臺日志如下:
修改Springboot配置文件:
# 找到Springboot的application.properties配置文件,新增以下配置 spring.servlet.multipart.enabled=true #是否啟用http上傳處理 spring.servlet.multipart.max-request-size=10MB #最大請求文件的大小 spring.servlet.multipart.max-file-size=10MB #設(shè)置單個文件最大長度
解決方案3:修改文件大小配置后,發(fā)現(xiàn)還是報405,這就懵了,繼續(xù)看后臺日志:
明白了,發(fā)現(xiàn)后端使用@RequestParam(“file”) 注解標注的MultipartFile參數(shù)沒有獲取到文件,對比前端請求參數(shù)名發(fā)現(xiàn)不一致造成的。
問題解決。
Springboot使用過程中遇到的一些問題
僅記錄個人遇到的一些問題及原因,和解決方案。
異常一
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at com.pjx.Application.main(Application.java:15) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:199) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:162) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
... 8 common frames omitted
Caused by: java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'
at org.springframework.boot.context.embedded.AbstractConfigurableEmbeddedServletContainer.checkContextPath(AbstractConfigurableEmbeddedServletContainer.java:132) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.context.embedded.AbstractConfigurableEmbeddedServletContainer.setContextPath(AbstractConfigurableEmbeddedServletContainer.java:120) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.autoconfigure.web.ServerProperties.customize(ServerProperties.java:198) ~[spring-boot-autoconfigure-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor.postProcessBeforeInitialization(EmbeddedServletContainerCustomizerBeanPostProcessor.java:73) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor.postProcessBeforeInitialization(EmbeddedServletContainerCustomizerBeanPostProcessor.java:59) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
... 16 common frames omitted
原因:配置項目名稱配置錯誤
錯誤配置:
server: context-path: admin
正確配置:
server: context-path: /admin
異常二:Mysql連接報錯
Could not create connection to database server
原因:MySql版本(8.0.28)和MySql驅(qū)動版本(5.1.45)不匹配,驅(qū)動版本換成8.0+就ok了。
使用:select version() from DUAL查詢mysql版本。
異常三:整合Druid密碼解密失敗
異常棧:Caused by: com.mysql.cj.exceptions.CJException: Access denied for user 'root'@'localhost' (using password: YES)
分析:密碼使用明文時,能正常連接數(shù)據(jù)庫,查詢到表數(shù)據(jù)。分析是密碼沒有成功解密。
pom添加的Druid依賴如下:
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency>
Druid解密是在ConfigFilter中init中進行的,斷點跟蹤如下圖,配置中的config.decrypt,config.decrypt.key是連在一起的,沒有分開,所以decrypt=false,沒有對密碼進行解密操作
檢查配置,數(shù)據(jù)庫配置如下圖
原因:connect-properties是druid-spring-boot-starter包下的配置參數(shù),沒有對properties進行解析處理,換成Druid報下的配置參數(shù)connection-properties。properties參數(shù)會進行按 ; 進行分隔處理,獲取到具體的配置信息。
解決方案:換成connection-properties即可
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java畢業(yè)設(shè)計實戰(zhàn)之醫(yī)院心理咨詢問診系統(tǒng)的實現(xiàn)
這是一個使用了java+Spring+Maven+mybatis+Vue+mysql開發(fā)的醫(yī)院心理咨詢問診系統(tǒng),是一個畢業(yè)設(shè)計的實戰(zhàn)練習,具有心理咨詢問診該有的所有功能,感興趣的朋友快來看看吧2022-01-01基于Java的Socket多客戶端Client-Server聊天程序的實現(xiàn)
這篇文章主要介紹了基于Java的Socket多客戶端Client-Server聊天程序的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03Springmvc應(yīng)用Mongodb分頁實現(xiàn)
這篇文章主要為大家詳細介紹了Springmvc應(yīng)用Mongodb分頁實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11